Anon
04-05-2008, 02:26 PM
void main()
{
//Recursion 2: Print Binary
cout << "The binary equivalent of 13 is: " << printBinary(13) << endl;
}
int printBinary(int n)
{
if (n > 0)
{
if ((n % 2) == 0)
{
printBinary(n /= 2);
cout << "0";
}
else
{
printBinary(n /= 2);
cout << "1";
}
}
return 5;
}
So im writing a simple recursive method to output the binary equivalent of a base 10 number. What I have works, however im lost on how I should return the number. What I have now is just a test with the return being 5 just as a placeholder. Any ideas on how to return the binary instead of just cout?
{
//Recursion 2: Print Binary
cout << "The binary equivalent of 13 is: " << printBinary(13) << endl;
}
int printBinary(int n)
{
if (n > 0)
{
if ((n % 2) == 0)
{
printBinary(n /= 2);
cout << "0";
}
else
{
printBinary(n /= 2);
cout << "1";
}
}
return 5;
}
So im writing a simple recursive method to output the binary equivalent of a base 10 number. What I have works, however im lost on how I should return the number. What I have now is just a test with the return being 5 just as a placeholder. Any ideas on how to return the binary instead of just cout?