I created a simple decimal-to-binary program.
Lets say I type in number 8.
It writes back 0001
and I want it to be 1000
how can I do that?
code here:
using namespace std;
int translating(int x);
int main()
{
int x;
int translate;
cout << "Write a number: ";
cin >> x;
cout << endl;
translate = translating(x);
cout << endl;
cout << endl;
return 0;
}
int translating(int x)
{
if (x == 1)
{
cout << "1";
return 0;
}
if ((x % 2)==1)
{
cout << "1";
return (translating((x-1)/2));
}
else
{
cout << "0";
return (translating(x/2));
}
}
Very simple, so simple you’ll kick yourself. Just reverse the order of your output statement and recursive function call. Also fixed a bug.