Requirements:
Write a C++ program which takes a 4-digit number (integer) as input from the user and prints the number in reverse order. For example, if the number entered is 5678 then the output should be 8765. The program should repeat this until the user decides to quit.
You need to pay attention to the following:
- Provide appropriate comments in your code
- Use meaningful identifiers
- Don't take the input as a sequence of characters. Take it as a single integer. Also, construct the output as a single integer, as opposed to printing it digit by digit.
- Your program should give a warning message if the number (value) is not a 4-digit number. For example, 0345 is not considered to be a 4-digit number.
- Your program should not be case-sensitive for the user choice i.e. both Q and q are should be acceptable quit.
- You may assume that when asked to enter a 4-digit number, the user enters a positive integer (as opposed to other kind of data, such as a character)
- When the last digit (units) of the input is 0, the reverse is not a 4-digit number. So it is Ok to report the reverse as a 3-digit number in that case.
Here is a typical dialogue for this program:
Input a 4 digit Number to Reverse and press Enter: 452
This is not a 4-digit number:452
Input a 4 digit Number to Reverse and press Enter: 56789
This is not a 4-digit number:56789
Input a 4 digit Number to Reverse and press Enter: 1432
New Reversed Number is: 2341
Enter Q to quit, enter any letter to continue n
Input a 4 digit Number to Reverse and press Enter: 4325
New Reversed Number is: 5234
Enter Q to quit, enter any letter to continue Q
Thanks for playing!
Important hint
- The integer % operator gives the remainder of a division of two values. For example: x = 11 % 3; results in x = 2;
- When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. For example: x = 11 / 3; results in x = 3.
- If the number entered is n, what does n%10 give you? How about n/10?
Turn in
- The code (as a cpp file),
- A file with the collected outputs from the trial inputs below the run outputs as requested below.
Coding Requirements:
- Follow the guidelines on Program Style described in section 2.5 of the textbook. ( See guidelines here).
Test Cases
Run the program for the following data, turning in a document will all run data (as a Word, text or pdf document)
Grading Table
Requirement |
Grading Comments |
Points |
Score |
Correct output on required trial data |
|
50 |
|
Good clean, easy to understand Input and output operations |
|
10 |
|
Complete source code |
|
10 |
|
Appropriate code formatting (indenting, whitespace) |
|
5 |
|
Good identifier names |
|
5 |
|
Description Comments on top |
|
5 |
|
Section comments throughout |
|
5 |
|
Document wth trial runs included |
|
10 |
|
Total |
|
100 |
|
Solution
Topic revision: r10 - 2019-09-05
- JimSkon