Nov 7, 2014 at 4:11pm UTC
I tired to make a c++ program that converts decimal numbers into binary, I got it right but the thing is when I input a number lets say 4 it gives the binary equivalent backwards 001 while it should give 100.
This is The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <iostream>
#include <string>
using namespace std;
int main()
{
int DecNumber;
cout << "Enter Decimal Number: \n" ;
cin >> DecNumber;
for (int beg = 1; beg <= 1; beg++)
{
if ( DecNumber % 2 == 0)
{
printf("0" );
beg--;
}
else
{
printf("1" );
beg--;
}
DecNumber = DecNumber / 2;
if ( DecNumber == 0 )
{
cout << '\n' ;
break ;
}
}
return 0;
}
Last edited on Nov 8, 2014 at 9:07am UTC
Nov 8, 2014 at 9:30am UTC
Thank you for replying and I did add them to a string and found another way with the <algorithm> that has the reverse function which is very helpful.
Is it bad to mix between stdout (printf) and stream (cout)?
And Thanks about the tip, i didn't know about the tags because I am new to this website.