#include // Write a program that queries the user for a // non-negative integer and then prints its digits // in reverse order. // // For example, if the user enters // // 395718860 // // then the program should print // // 068817593 // // If the user enters a negative integer, simply // return 0. // // There are two natural approaches: // // 1. Calculate an integer whose digits are in // reverse order and then print that integer. // // 2. Print the digits in reverse order as they // are extracted. // // Two possible solutions are given below. There is // an important difference between the two approaches. // Try to determine what it is. // Function declarations int main(); // Function definitions int main() { int m{}; std::cout << "Enter a non-negative integer: "; std::cin >> m; std::cout << m << std::endl; return 0; } // Hint: // // The operators // // % // // and // // / // // may be useful. // // In particular, if m is a non-negative integer, // what are // // m%10 // // and // // m/10? // // How could these operations be used repeatedly to // extract the digits from right to left? // Here is one solution. // // This solution first calculates the reversed integer // and then prints it: /* int main() { int m{}; std::cout << "Enter a non-negative integer: "; std::cin >> m; if ( m < 0 ) { return 0; } int reverse{ 0 }; while ( m != 0 ) { reverse *= 10; reverse += m%10; m /= 10; } std::cout << reverse << std::endl; return 0; } */ // Here is another solution. // // This solution does not calculate a reversed integer. // Instead, it prints each digit as it is extracted: /* int main() { int m{}; std::cout << "Enter a non-negative integer: "; std::cin >> m; if ( m < 0 ) { return 0; } if ( m == 0 ) { std::cout << 0 << std::endl; } else { while ( m != 0 ) { std::cout << m%10; m /= 10; } std::cout << std::endl; } return 0; } */ // PROBLEM: // // What happens if you enter // // 10 // // with each of these solutions? // // The desired output is // // 01. // // Which solution produces this output? // // Why is the other solution unable to represent // the leading zero? // // What happens with a number such as // // 12000? // // How could you modify the first solution so that // it produces exactly the same output as the second // solution? // Hint: // // An integer cannot contain leading zeros. // // For example, the integer represented by // // 01 // // is simply the integer // // 1. // // Therefore, if you first calculate the reversed // integer, you must separately remember how many // zeros need to be printed before that integer. // // Which zeros in the original number become leading // zeros when its digits are reversed? // Here is one solution: /* int main() { int m{}; std::cout << "Enter a non-negative integer: "; std::cin >> m; if ( m < 0 ) { return 0; } if ( m == 0 ) { std::cout << 0 << std::endl; return 0; } int trailing_zeros{ 0 }; bool non_zero_digit_found{ false }; int reverse{ 0 }; while ( m != 0 ) { reverse *= 10; if ( !non_zero_digit_found && (m%10 == 0) ) { ++trailing_zeros; } else { non_zero_digit_found = true; } reverse += m%10; m /= 10; } for ( int k{ 0 }; k < trailing_zeros; ++k ) { std::cout << 0; } std::cout << reverse << std::endl; return 0; } */