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

((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...
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...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is...
C++ PROGRAMING Implement a program to evaluate simple mathematical expressions. Assume that the original expression is provided to the program as a text string. Allowed expression tokens: brackets “(” and “)”, integers like “4”, “15”, addition “+”, subtraction “-”, multiplication “*”, division “/”. Output can be float. Trim spaces from an expression. Brackets should contain at least one operation. Make sure that an expression is validated before it is calculated; reject the invalid expressions with appropriate message. The program must...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of...
This is C++ programing Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.Given an array a, an int variable n containing the number of elements in a, and two other intvariables, k and temp, write a loop that reverses the elements of the array.Do not use any other variables besides...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT