I wrote a program that outputs the binary number of a decimal number passed to it, however the binary number is out putted backwards. So the binary of 25 is 11001 but my program gives 10011. Can any one help me? Thanks in advance!
#include <iostream>
void hi( unsignedint x )
{
constint last_digit = x % 2 ; // hold the rightmost digit
if( x > 1 ) hi( x/2 ) ; // first print the binary digits to the left
std::cout << last_digit ; // and then print the rightmost digit
}
int main()
{
constunsignedint n = 25 ;
hi(n) ; // prints 11001
std::cout << '\n' ;
}
#include <iostream>
void DecToBinaryOutput( unsignedint x )
{
unsignedint bit = x % 2;
x /= 2;
if ( x ) DecToBinaryOutput( x );
std::cout << bit;
}
int main()
{
std::cout << "Enter a non-negative number: ";
unsignedint x = 0;
std::cin >> x;
DecToBinaryOutput( x );
std::cout << std::endl;
}