/* File binrep1.cpp Version 1 avoids an array altogether and uses an an explicitly recursive function instead. J. Shinnerl, 10/18/99; 1/29/02 This program prints out the binary digits of an integer entered by the user at run time. Because the digits are acquired by integer division using %, they are obtained in reverse order. In order to print them out in the correct order, the digits are reversed again during printing by delaying the print until after a recursive call. */ #include #include using std::cout; using std::cin; using std::endl; // Note: n is received "by value"; void binaryPrint( int n ) // hence it is a local variable, *not* shared { // with the calling function. if ( n > 1 ) binaryPrint(n/2); // First, print the preceding digits, if any. cout << n%2; // Second, print the last digit. } int main() { cout << "\nThis program prints out the binary digits " << "of a nonnegative integer."; cout << "\nEnter a nonnegative integer: "; int number, count=0; cin >> number; if (number < 0){ cout << "The number must be nonnegative!\n"; return EXIT_FAILURE; } binaryPrint(number); cout << endl << endl; return EXIT_SUCCESS; }