Answer to Question #35138 in C++ for samusha

Question #35138
c++ program to convert decimal to binary using recursion
1
Expert's answer
2013-09-18T09:31:13-0400
#include <iostream>
#include <string>

using namespace std;


string dec_to_bin(int decNumber)
{
/* Convert positive numbers only */
if (decNumber <= 0)
return "0";

static string result;
char digit;

if (decNumber != 0)
{
digit = (decNumber % 2) + '0';
result.insert(0, &digit, 1);
dec_to_bin(decNumber / 2);
}
return result;
}


int main()
{
int decNumber;


cout << "Enter a decimal number: ";
cin >> decNumber;
cout << "The binary value is: " << dec_to_bin(decNumber);


return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS