# Program to Reverse a Number in C

Reverse a Number in c:
This is an example of c program to reverse a number using –
1. While Loop
2. Function
3. Recursion
Program to Reverse a Number in C language is explained properly with algorithm (logic) and example. If the number entered by the user is 231 then its reverse will be 132 and this output is printed on the screen.

You should have the knowledge of following C Programming topics to understand the program for reverse a number in c better –

• C operator
• C while and do…while loop
• C Functions

## Reverse a Number in C Algorithm

An algorithm is the logic of the program. If we understand the logic of the program then it becomes easy to write any c program.
Here, is the algorithm to find Reverse of a number in C Programming.

1. First, we will take four variables: n, rev, a and old.
2. Input a number from the user and store it in variable n
3. Initialise rev=0 as rev variable stores reverse of the number entered by the user.
4. Store value of n entered by the user in variable old ,i.e., old=n as value of n is changing during calculation.
5. Run for loop to find reverse of a number in c.
6. And variable a is used to store the remainder.

Note: % is Modulus which returns remainder of a division. In this c program to find reverse of a number we have used  % to find the last digit of the number n.

``````START
Step 1 → Take integer variable N, A, REV=0, OLD
Step 3 → OLD=N
Step 4 → REPEAT STEPS 4 TO 7 WHILE N!=0:
Step 5 → A = N % 10
Step 6 → N = N / 10
Step 7 → REV = REV * 10 + A
Step 8 → DISPLAY reverse of the number N
STOP
``````

## Program to Reverse a Number in C using While Loop

``````
/* Program to Reverse a Number in C */

#include  //printf() & scanf() Header File
#include  //clrscr() & getch() Header File

int main()
{
int n, rev, a, old;

clrscr(); // to clear output screen

/* To take number from the user */
printf("Enter a number: ");
scanf("%d", &n);

rev = 0;
old = n;

while(n!=0)
{
a = n % 10;
n = n / 10;
rev = rev * 10 + a;
}

printf("Reverse of %d = %d", old, rev);

getch(); //to hold output screen
return(0);
}
``````

Output –

``````Enter a number: 417
Reverse of 417 = 714``````

### Reverse Number Program in C Explanation

Let the number entered by the user is 417 ,i.e., n=417
Initialise rev = 0
while(n!=0)    /* for loop is executed till n is not equal to 0 */

1st Iteration: while(417!=0)    /* condition is true */
```a = n % 10
= 417 % 10
= 7

n = n / 10
= 417 / 10
= 41

rev = rev * 10 + a
= 0 * 10 + 7
= 7
```
2nd Iteration: while(41!=0)    /* condition is true */
```a = n % 10
= 41 % 10
= 1

n = n / 10
= 41 / 10
= 4

rev = rev * 10 + a
= 7 * 10 + 1
= 70 + 1
= 71
```
3rd Iteration: while(4!=0)    /* condition is true */
```a = n % 10
= 4 % 10
= 4

n = n / 10
= 4 / 10
= 0

rev = rev * 10 + a
= 71 * 10 + 4
= 710 + 4
= 714
```
So, reverse of 417 is 714

## Program to Reverse a Number in C using Function

``````
/* Program to Reverse a Number in C using Function */

#include <stdio.h> //printf() & scanf() Header File
#include <conio.h> //clrscr() & getch() Header File

/* function declaration */
void reverse_number(int);

int main()
{
int n;

clrscr(); // to clear output screen

/* To take number from the user */
printf("Enter a number: ");
scanf("%d", &n);

/* calling function */
reverse_number(n);

getch(); //to hold output screen
return(0);
}

void reverse_number(int n)
{
int rev, a, old;

rev = 0;
old = n;

while(n!=0)
{
a = n % 10;
n = n / 10;
rev = rev * 10 + a;
}

printf("Reverse of %d = %d", old, rev);
}
``````

Output –

``````Enter a number: 47
Reverse of 47 = 74``````