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

In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL...
In C++ Complete the template program. ADD to your c++ program as a comment the PARTIAL output from executing your program - Only copy the last 6 lines of output. There is no input data for this problem. // Find Pythagorean triples using brute force computing. #include <iostream> using std::cout; using std::endl; int main() { int count = 0; // number of triples found long int hypotenuseSquared; // hypotenuse squared long int sidesSquared; // sum of squares of sides cout...
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...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of each word A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data as described below. Variables to hold the data read in using std::cin and a return statement. #include <iostream > int main(int argc, char *argv[]) { .... your code goes here }//main Example usage: >A01.exe Enter three 3-letter space separated words, then...
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...
write a simple c program to reverse pairs of characters in a string. For exaple, "notified"...
write a simple c program to reverse pairs of characters in a string. For exaple, "notified" becomes "edfitino". The reversal should be in this order in a simple easy to understand c program.
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a...
SOLVE IN C: 6.31 LAB: Print string in reverse Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters.The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH Hint: Use the fgets function to...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT