# C Program to Find Reverse of a Number

Write a C program to find the reverse of a number. For example, the reverse of number 7854 is given as 4587.

From the operators topic, we know that the modulus operator (%) can be used to find the last digit of the number. Similarly, the division operator (/) can be used to remove the last digit of the number.

Steps to reverse a number,

Declare a variable reverse, and initialize it with 0.
Step1:- Find the last digit of the number. lastDigit = number%10
Step2:- Append this lastDigit to the variable reverse. reverse = (reverse*10)+lastdigit;
Step3:- Remove the last digit of the number. number = number/10
Repeat this process (from step1 to step3) until the number becomes 0.

## C Program to Find Reverse of a Number using while loop

`````` #include<stdio.h>
int main()
{
int number, lastDigit, reverse=0;

printf("Enter a number: ");
scanf("%d",&number);

while( number!=0 )
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
number /= 10;
}

printf("Reverse Number = %d\n",reverse);

return 0;
}``````

Output:-

Enter a number: 12345
Reverse Number = 54321

Enter a number: 0032
Reverse Number = 23

Enter a number: 123000
Reverse Number = 321

In this program, the input number is stored in the variable `number`. To reverse the number modulus and division operators are used, and the result is stored in the variable `reverse`.

### Another Way

This method is a shortcut way and it can’t be used with big programs. Thi method after finding the last digit, it is displayed to the screen without storing it. Each iteration display one digit. The below program displays the number in reverse. Don’t use this method in another program.

`````` #include<stdio.h>
int main()
{
int number, lastDigit;

printf("Enter a number: ");
scanf("%d",&number);

while( number!=0 )
{
lastDigit = number%10;
//displaying within while loop
printf("%d",lastDigit);
number /= 10;
}

return 0;
}``````

Output for different test-cases:-

Enter a number: 852
258

Enter a number: 123000
000321

Enter a number: 000032
23

## C Program to Find Reverse of a Number using the do-while loop

`````` #include<stdio.h>
int main()
{
int number, lastDigit, reverse=0;

printf("Enter a number: ");
scanf("%d",&number);

do
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
number /= 10;
}while( number!=0 );

printf("Reverse Number = %d\n",reverse);

return 0;
}``````

## C Program to Find Reverse of a Number using for loop

`````` #include<stdio.h>
int main()
{
int number, lastDigit, reverse;

printf("Enter a number: ");
scanf("%d",&number);

for(reverse=0; number!=0; number /= 10)
{
lastDigit = number%10;
reverse = (reverse*10) + lastDigit;
}

printf("Reverse Number = %d\n",reverse);

return 0;
}``````

In this program reverse variable is declare before for loop becomes it is used outside of the for loop (in the last statement). If we declare the reverse variable at the time of initialization of for loop then the variable becomes local variable and can be accessed only within the `for `loop. You can read more about it in Scope and lifetime of a variable.

Output for the different test-cases:-

Enter a number: 12345
Reverse Number = 54321

Enter a number: 00123
Reverse Number = 321

Enter a number: 2145000
Reverse Number = 5412

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!