In: Computer Science
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value.
For Example:
don't use this code as part of the answer
string s = d == 0 ? "0" : "1";
#include <iostream> #include <string> using namespace std; string decimalToBinaryRecursive(int n) { if(n == 0) { return "0"; } else if(n == 1) { return "1"; } else { int d = n % 2; string s; if (d == 0) { s = "0"; } else { s = "1"; } return decimalToBinaryRecursive(n/2) + s; } } int main() { cout << decimalToBinaryRecursive(0) << endl; cout << decimalToBinaryRecursive(1) << endl; cout << decimalToBinaryRecursive(8) << endl; return 0; }