C++
Write a function that returns a string of 1's and 0's. This needs
to be the binary representation of a number.
Do not use loops. Use only recursion. Do not change the function's
parameters or add more parameters. Do not change the main
program.
#include
#include
string bin(int number)
{
//Code here
}
int main()
{
cout << bin(43) << endl; // Should be
101011
return 0;
}
#include <iostream> #include <string> using namespace std; string bin(int number) { if (number == 0) { return "0"; } else if (number == 1) { return "1"; } else { string s = bin(number / 2); if (number % 2 == 0) { s += "0"; } else { s += "1"; } return s; } } int main() { cout << bin(43) << endl; // Should be 101011 return 0; }
Get Answers For Free
Most questions answered within 1 hours.