Hello. I'm very new to C++ and can't seem to figure out a seemingly simple problem. Using only loops (for, while, do while, etc.) I need to take user inputted numbers and output them reversed. For example, if a user enters 12345, I need the program to output 54321 or if they enter 077 I need it to output 770.
You can:
- Read the input as a string and print the characters in reverse order
- Read the input as a number, convert it to a string and print the characters in reverse order
- Read the string as a number and use the modulus ( % ) to show the single digits ( n % 10 returns the last digit of 'n' )
I can't use any string manipulators (reverse), but I can use the modulus operator. But how would I use that in a string if it only returns the last digit entered? Thanks
You don't need much more than a loop to display a string in a reverse mode.
For the numbers is even simpler, take this example:
The user enters 1234 ( stored in an integral type )
1234 % 10 returns the last digit ( 4 )
1234 / 10 = 123.4 but 123 and 10 are integers and the result would be truncated so it will be 123
...