In: Computer Science
Reversing Digits - Complete the template program in C++ (Be Original)
// reverseNumber.cpp // Reverse the digits of a number. #include <iostream> using std::cin; using std::cout; using std::endl; #include <iomanip> using std::setw; /* Write prototype for reverseDigits */ // STUDENT WRITES CODE HERE int main() { int number; // input number cout << "Enter a number between 1 and 9999: "; cin >> number; cout << "The number with its digits reversed is: "; // find number with digits reversed // STUDENT WRITES CODE HERE *************** cout << /* Write call to reverseDigits */ << endl; return 0; // indicate successful termination } // end main // reverseDigits returns number obtained by reversing digits of n /* Write function header for reverseDigits */ // STUDENT WRITES CODE BELOW *************** { int reverse = 0; // reversed number int divisor = 1000; // current divisor int multiplier = 1; // current multiplier // loop until single-digit number while ( n > 9 ) { // if n >= current divisor, determine digit if ( n >= divisor ) { // update reversed number with current digit reverse += n / divisor * multiplier; n %= divisor; // update n /* Write a line of code that reduces divisor by a factor of 10 */ // STUDENT WRITES CODE HERE *************** /* Write a line of code that increases multiplier by a factor of 10 */ // STUDENT WRITES CODE HERE *************** } // end if else // else, no digit divisor /= 10; // update divisor } // end while reverse += n * multiplier; return reverse; // return reversed number } // end function reverseDigits
// do comment if any problem arises
//code
// reverseNumber.cpp
// Reverse the digits of a number.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
/* Write prototype for reverseDigits */
// STUDENT WRITES CODE HERE
int reverseDigits(int);
int main()
{
int number; // input number
cout << "Enter a number between 1 and 9999: ";
cin >> number;
cout << "The number with its digits reversed is: ";
// find number with digits reversed
// STUDENT WRITES CODE HERE ***************
cout << reverseDigits(number) << endl;
return 0; // indicate successful termination
} // end main
// reverseDigits returns number obtained by reversing digits of n
/* Write function header for reverseDigits */
// STUDENT WRITES CODE BELOW ***************
int reverseDigits(int n)
{
int reverse = 0; // reversed number
int divisor = 1000; // current divisor
int multiplier = 1; // current multiplier
// loop until single-digit number
while (n > 9)
{
// if n >= current divisor, determine digit
if (n >= divisor)
{
// update reversed number with current digit
reverse += n / divisor * multiplier;
n %= divisor; // update n
/* Write a line of code that reduces divisor by a factor of 10 */
// STUDENT WRITES CODE HERE ***************
divisor /= 10;
/* Write a line of code that increases multiplier by a factor of 10 */
// STUDENT WRITES CODE HERE ***************
multiplier *= 10;
} // end if
else // else, no digit
divisor /= 10; // update divisor
} // end while
reverse += n * multiplier;
return reverse; // return reversed number
} // end function reverseDigits
Output: