**Reverse Number** is another easy and simple program which mostly asked in exams.

In this tutorial, we are going to learn how to reverse a number programmatically.

**Reverse Number Logic**

How to Reverse Number in c?

First, we have the given number.

To reverse number in c we perform following steps

First to reverse number we separate out the given number.

We separate digits in number by modulus operation which gives us a digit in a number.

**Rem = number % 10**

After separating number we store that digit in another variable with considering its unit places which we perform as

**Reverse = Reverse × 10 + Remember**

We repeated step 1 and 2 till number ends.

**Concept you require to know from C :-**

**Example to reverse number :-**

Number = 365

Now reverse number by following above steps

**1. Separate out the digits in a number**

Rem = number % 10

Rem = 365 % 10

Rem = 5

**2. Store the reverse number in a variable**

Reverse = Reverse × 10 + Remember

Reverse= 0 × 10 + 5

Reverse = 5

**3. Calculate remaining digits in a number**

Number = number / 10

Number = 365 / 10

Number = 36

**4. Do same as step 1 and 2 and 3**

Rem = number % 10

Rem = 36 % 10

Rem = 6

**5. Store the reverse number in a variable**

Reverse = Reverse × 10 + Remember

Reverse= 5 × 10 + 6

Reverse = 56

**6. Calculate remaining digits in a number**

Number = number / 10

Number = 3 / 10

Number = 3

**7. Do same as step 1 and 2 and 3**

Rem = number % 10

Rem = 3 % 10

Rem = 3

**8. Store the reverse number in a variable**

Reverse = Reverse × 10 + Remember

Reverse= 56 × 10 + 3

Reverse = 563

### Program to reverse number in c using while loop

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<stdio.h> void main() { int temporary ,n,rev=0,x; printf("Enter a number\n"); scanf("%d",&n); temporary =n; while(n>0) { x=n%10; rev=rev*10+x; n=n/10; } printf("The reverse of %d is %d\n",temporary ,rev); } |

**Output :-**

**Explanation :-**

**1. Here we did initialization for**

temporary ->To store the entered value(i.e ‘n’) as you will come to know at the end of the program

n->To store number given by a user.

rev->To store the reverse of a number.It is initialized to zero

x->To store n%10.

**2. First of all, we got a number ‘n’ from a user and then stored it in a temporary variable called as ‘temporary ‘ for restoring the value.(remember this point).**

**3. Now the main logic comes here**

let the number ‘n’ be 321 and as 321>0, while loop gets executed

then x=321%10—>which is 1.

rev=0*10+1——–>1

n=321/10———>32

The rev for the first loop execution is rev=1.

**4. Now the number ‘n’ has become ’32’ and n>0, while loop executes for the 2nd time.**

then x=32%10—>which is 2.

rev=1*10+2——–>12

n=32/10———>3

The rev when loop executed the second time is rev=12.

**5. Now the number ‘n’ has become ‘3’ and n>0, while loop executes for the 3rd time**

then x=3%10—>which is 3.

rev=12*10+3——–>123

n=3/10———>0

The rev when loop executed the third time is rev=123.

**6. Now as the number of variable ‘n’ is 0 which is not n>0 then the loop terminates.Then the final reverse is ‘123’.**

**7. So now I hope you understood why the temporary variable is used.It is because the value in ‘n’ becomes 0 at the end of the program so for restoring this value to print at the end we used ‘temporary ‘(as from the 2nd point).**

**8. Finally, it prints the value in ‘rev’.**

## Leave a Reply