Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a C++ program that prompts a user to enter a four-digit integer and prints the number in reverse. HINT: Print one digit at a time, starting from the right of the number.

Sample run of program:

Enter a four-digit number: 3412
Your number in reverse is 2143

I couldn't figure out the last part, which was flipping the last number.


What I have tried:

i got most of it done i just can figure out how to flip the last number. how would i do this.

could you actually walk me through it tho im tryna learn.
Posted
Updated 16-Feb-23 22:16pm

Start by thinking "how would I do that manually?"
Write the number on a blackboard then take the lowest digit out and write that down, then rub it out.
If there are any digits left you do it again until there aren't.

So you need three things:
1) A loop that stops when you run out of digits. Hint: think about the do...while loop
2) A way to extract the least significant digit. Hint: what does the modulus operator do?
3) A way to remove the least significant digit. Hint: how does integer division differ from floating point division?

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
CPallini 17-Feb-23 2:10am    
5.
Andre Oosthuizen 17-Feb-23 4:42am    
4+x=5
Try with starting some Learn C++ tutorial or some video tutorial.

I strongly recommend that you take the time to read my tips to learn from my experiences in that industry.

some tips: use functions and classes with understandable names and install some IDE like Visual Studio.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900