Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wrote a program for my CS class in C to print me the reverse of a number

#include <stdio.h>
int main()
  int n, r = 0;
  scanf("%d", &n);

    while (n != 0)
    r = r * 10;
    r = r + n%10;
    n = n/10;

  printf("The reverse of %d is %d\n", n , r);

  return 0;

I am getting the result as lets say for number 123 : " The reverse of 0 is 321 "

What I have tried:

My code seems fine i dont get it why it s not working i ve checked anything
Updated 26-Nov-22 15:55pm
0x01AA 26-Nov-22 6:06am    
This is because you should save the original input or in other words make a working copy of 'n' and work with that copy. This because of n = n/10; ;)
0x01AA 26-Nov-22 6:13am    
The other thing you have to think about is, what happens if you enter the number '100'. That would be a little challenge ;)

As 0x01AA has shown you by example, your approach has a number of flaws.
Instead of trying to build up an "output number" - print each digit as you extract it.

First print human readable text and input number without a new line.
Then enter a loop and print the digits in turn.
After the loop ends, print a newline.

I'd suggest that you think about using a do ... while loop instead of a while. See if you can work out why before you go any further!
Share this answer
0x01AA 26-Nov-22 9:56am    
do ...while vs. while, I think it does not matter. When you enter that 'magic' one digit (which I think you have in your mind) it still works, or I miss something ;)
Rick York 26-Nov-22 12:48pm    
A do-while loop will always execute at least once. A while loop may not.
0x01AA 26-Nov-22 14:44pm    
I'm pretty aware about that
0x01AA already gave you the solution.
You could, as well, write a function to compute the reverse of the number and then call it:
  printf("The reverse of %d is %d\n", n, reverse(n));
Share this answer
My code seems fine i dont get it why it s not working i ve checked anything

there is a difference between what you think your code is doing and reality. May be time to throw an eye under the hood and see exactly what is going on, the tool of choice is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

By the way, using input as a number may be the wrong approach.
if input is 1200, what output do you expect ?
21 or 0021 ?

think how to handle words and get letters in reverse order.
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