This C++ Program demonstrates the Conversion of a Number from a Decimal Base to Any Base less than the Number.
Here is source code of the C++ Program to Convert a Number from Decimal Base to Any Base. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to Convert a Decimal Base to Any Base
*/
#include<iostream>
#include <cstdio>
using namespace std;
/*
* Convert a Decimal Base to Any Base
*/
void convert10tob(int N, int b)
{
if (N == 0)
return;
int x = N % b;
N /= b;
if (x < 0)
N += 1;
convert10tob(N, b);
cout<< x < 0 ? x + (b * -1) : x;
return;
}
/*
* Main
*/
int main()
{
int N,b;
cout<<"Enter the integer to convert(N): ";
cin>>N;
cout<<"Enter the base <= N: ";
cin>>b;
if (N != 0)
{
convert10tob(N, b);
cout<<endl;
}
else
cout<<"0"<<endl;
return 0;
}
$ g++ decimal_any.cpp $ a.out Enter the integer to convert(N): 100 Enter the base >= N: 2 1100100 ------------------ (program exited with code: 1) Press return to continue
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Programming MCQs
- Check Programming Books
- Check C++ Books
- Check Computer Science Books
- Practice Computer Science MCQs