# Reverse a Number Program

Written By -

Reverse a number in C:

The code for reversing a number in C is:

```#include <stdio.h>

int  main()

{

int  num;

int  reversedNum = 0;

int  remainder;

printf("Enter an integer: ");

scanf("%d", & amp; num);

while (num != 0)

{

remainder = num % 10;

reversedNum = reversedNum * 10  + remainder;

num = num / 10;

}

printf("Reversed Number = %d", reversedNum);

return  0;

}
```

The inputs and outputs for the above code are:

```Enter an integer:  1234

Reversed Number = 4321

Enter an integer:  456

Reversed Number = 654

Enter an integer:  905

Reversed Number = 509
```
• We have created the variable num to store the user input and the variable reversedNum to store the reverse of the number input by the user.
• The while loop runs until num is not equal to 0.
• In each iteration, we calculate the remainder when num is divided by 10. The value of num is then reduced by 10 times.
• At the end of the loop, we get the reversed number.

When we input 1234 for the above code, all the intermediate steps will be:

```Enter an integer:  1234

Before iteration:

num: 1234

During iteration:

remainder: 4

reversedNum: 4

num: 123

remainder: 3

reversedNum: 43

num: 12

remainder: 2

reversedNum: 432

num: 1

remainder: 1

reversedNum: 4321

num: 0

After iteration:

Reversed Number = 4321
```

Related Posts: