// // Example 6.9. Program to print the digits of a // nonnegative input integer in reverse order // #include using namespace std; int main() { int readNum(); void printReverse(int num); int num; // nonnegative input integer num = readNum(); printReverse(num); return 0; } // // Function to print instructions and repeatedly read // integers until the user enters a nonnegative integer, // which the function returns // Pre: none // Post: A nonnegative integer has been returned. // int readNum() { int num; // nonnegative input integer cout << "This program prints a nonnegative integer backwards." << endl << endl; do { cout << "Enter a nonnegative integer: "; cin >> num; } while (num < 0); return num; } // // Function to print the digits of a nonnegative integer // in reverse order // Pre: num is a nonnegative integer. // Post: The digits of num have been displayed in reverse order. // void printReverse(int num) { int rightmostDigit; // rightmost digit of num cout << num << " backwards is "; do { rightmostDigit = num % 10; cout << rightmostDigit; num /= 10; // move next digit into rightmost position } while (num > 0); cout << endl; }