// binarystring.cpp // Demonstrates recursive conversion to binary for CS 24 // cmc, 5/11/2010 #include #include #include // for stringstream using namespace std; // utility converts num to string string numString(int num) { stringstream result; result << num; return result.str(); } // recursive function to convert n to binary string string getBinary (int n) { if (n <= 1) return numString(n); return getBinary (n / 2) + numString (n % 2); } int main() { int value; cout << "enter integers (0 to quit)" << endl; cin >> value; while (value > 0) { cout << getBinary(value) << endl; cin >> value; } return 0; }