Reverse printing a number using a loop
Printing a number in reverse order of digit by using digit extraction technique in a loop
Learning Objectives
- A loop technique to manipulate printing inside a loop so that display of desired reversed number can be achieved without actually storing the number.
Source Code
|
|
Related ( ? ) : |
Run Output
Notes
- One can write the same program in many other ways where reversed number is stored in another variable and then printed. This is a better way when you need to use the reversed number in some other calculation after the loop.
- Before starting the loop in this case we have not copied the input number to another number as we have not printed the original number after the loop. Although it is always a good idea to do so.
Common Errors
- Take care of not writing any endl or newline character while printing the digit as in this case you do not want any gap to be printed between the digits while printing the number in the reversed order.
Suggested Filename(s): rev-num.cpp
Created: 2-Jan-2018 22:24 | Updated: 15-Sep-2018 12:44|
