Question

In: Computer Science

Reversing Digits - Complete the template program in C++ (Be Original) // reverseNumber.cpp // Reverse the...

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

Solutions

Expert Solution

// 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:


Related Solutions

Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
Program Zeus:    Complete the Count Vowels program (Count Vowels.cpp template provided at the end) to include two...
Program Zeus:    Complete the Count Vowels program (Count Vowels.cpp template provided at the end) to include two user defined functions (which you will write!). Code lenguage is C++ bool isVowel (char c)       // returns true if c is a vowel and false otherwise int countVowels (string s) // returns the number of vowels in s. You can access each character of s by using s.at(i) where i ranges from 0 to s.length()-1. countVowels () should call isVowel (s.at(i)) to check...
C++, Complete this program as directed // This program will read in a group of test...
C++, Complete this program as directed // This program will read in a group of test scores (positive integers from 1 to 100) // from the keyboard and then calculate and output the average score // as well as the highest and lowest score. There will be a maximum of 100 scores. // PLACE YOUR NAME HERE #include <iostream> using namespace std; typedef int GradeType[100]; // declares a new data type: // an integer array of 100 elements float findAverage...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will...
REVERSE POLISH CALCULATOR C++ ONLY. For this assignment, you are to write a program, which will calculate the results of Reverse Polish expressions that are provided by the user. You must use a linked list to maintain the stack for this program (NO array implementations of the stack). You must handle the following situations (errors): Too many operators (+ - / *) Too many operands (doubles) Division by zero The program will take in a Polish expression that separates the...
For C++ Download the attached program template. The programs calculates the amount of profit or loss...
For C++ Download the attached program template. The programs calculates the amount of profit or loss from a stock transaction. Using the values initialized by the program, calculate and print out the following results: "Total paid for stock" "Purchase commission " "Total received for stock" "Sales commission" "Amount of profit or loss" Use the following formulas: Total_paid = (total purchase price) + purchase_commission; Total_received = (total sales price) - sales_commission; profit = Total_received - Total_paid /* * COSC 1337 Programming...
Write a program to reverse the lines of a file and to reverse the words plus...
Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList. A file name mobydick.txt Example: Original file contains the following MOBY DICK; OR THE WHALE by Herman Melville CHAPTER 1 Loomings. Out put should be .sgnimooL 1 RETPAHC ellivleM namreH yb ELAHW EHT RO ;KCID YBOM its for java eclipse
Original C code please. Part 1: You can do A, B, and C in one program...
Original C code please. Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are evenly divisible by both 2 and 7. 14        28       ...
In C program. Read and convert a sequence of digits to its equivalent integer. Any leading...
In C program. Read and convert a sequence of digits to its equivalent integer. Any leading white space should be skipped. The conversion should include digit characters until a non-digit character is encountered. Modify the program so it can read and convert a sequence of digit characters preceded by a sign, + or -.
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT