Write a C program that accepts as inputs a base 10 integer n

Write a C++ program that accepts as inputs a base 10 integer number.

Convert that number into a base 2, 8 and 16 number.

Solution

#include<iostream>
using namespace std;

void convert(int base10, int base)
{
if (base10 == 0)
return;
int n = base10 % base;
base10 /= base;
if (n < 0)
base10 += 1;
convert(base10, base);
cout<< n < 0 ? n + (base * -1) : n;
return;
}
int main()
{
int base10,base;
cout<<\"Enter the base 10 integer number: \";
cin>>base10;
cout<<\"Enter the base to convert: \";
cin>>base;
convert(base10, base);
cout<<endl;
  
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the base 10 integer number: 100                                                                                                                                                                                                                  

Enter the base to convert: 2                                                                                                                                                                                                                           

1100100                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the base 10 integer number: 100                                                                                                                                                                                                                  

Enter the base to convert: 8                                                                                                                                                                                                                           

144                                                                                                                                                                                                                                                    

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the base 10 integer number: 100                                                                                                                                                                                                                  

Enter the base to convert: 16                                                                                                                                                                                                                          

64

Write a C++ program that accepts as inputs a base 10 integer number. Convert that number into a base 2, 8 and 16 number.Solution#include<iostream> using n
Write a C++ program that accepts as inputs a base 10 integer number. Convert that number into a base 2, 8 and 16 number.Solution#include<iostream> using n

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site