64 659
Assignments Done
99,2%
Successfully Done
In September 2018

Answer to Question #35138 in C++ for samusha

Question #35138
c++ program to convert decimal to binary using recursion
Expert's answer
#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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions