# C Program to Reverse a Number

How to write a C Program to Reverse a Number using While Loop, Functions, and Recursion?.

## C Program to Reverse a Number Using While Loop

This C program to reverse a number allows the user to enter any positive integer, and then this C program will reverse a number using While Loop

```/* C Program to Reverse a Number Using While Loop */

#include <stdio.h>

int main()
{
int Number, Reminder, Reverse = 0;

printf("\nPlease Enter any number to Reverse\n");
scanf("%d", & Number);

while (Number > 0)
{
Reminder = Number %10;
Reverse = Reverse *10+ Reminder;
Number = Number /10;
}

printf("Reverse of entered number is = %d\n", Reverse);

return 0;
}```

This program for reverse a number in c allows the user to enter any positive integer, and then that number assigned to variable Number. Next, Condition in the While Loop will make sure that the given number is greater than 0

From the above reverse a number in c example, User Entered value: Number = 1456 and Reverse = 0

First Iteration
Reminder = Number %10
Reminder = 1456%10 = 6

Reverse = Reverse *10+ Reminder
Reverse = 0 * 10 + 6 => 0 + 6 => 6

Number = Number /10
Number = 1456 /10 => 145

Second Iteration
From the first Iteration of this C Program to Reverse a Number, the values of both Number and Reverse has changed as Number = 145 and Reverse = 6

Reminder = Number % 10
Reminder = 145 % 10 = 5

Reverse = Reverse *10+ Reminder
Reverse = 6 * 10 + 5 => 60 + 5  => 65

Number = Number /10
Number = 145 /10 => 14

Third Iteration
From the Second Iteration of this C program, the values of both Number and Reverse changed as Number = 14 and Reverse = 65

Reminder = Number %10
Reminder = 14%10 = 4

Reverse = Reverse *10+ Reminder
Reverse = 65 * 10 + 4 => 650 + 4 => 654

Number = Number /10 => 14/10
Number = 1

Fourth Iteration
From the third Iteration of reverse a number in c program, the values of both Number and Reverse has changed as Number = 1 and Reverse = 654

Reminder = Number %10
Reminder = 1 %10 = 1

Reverse = Reverse *10+ Reminder
Reverse = 654 * 10 + 1 => 6540 + 1 => 6541

Number = Number /10
Number = 1/10 => 0

Here, For the next iteration, Number = 0. So, the C Programming while loop condition will fail

## C Program to Reverse a Number Using Functions

This reverse a number program allows the user to enter any positive integer. And then, this C program reverse a number using Functions

```/* C Program to Reverse a Number Using Functions */
#include <stdio.h>

int Reverse_Integer (int);

int main()
{
int Number, Reverse = 0;

printf("\nPlease Enter any number to Reverse\n");
scanf("%d", &Number);

Reverse = Reverse_Integer (Number);

printf("Reverse of entered number is = %d\n", Reverse);

return 0;
}

int Reverse_Integer (int Number)
{
int Reminder, Reverse = 0;

while (Number > 0)
{
Reminder = Number %10;
Reverse = Reverse *10+ Reminder;
Number = Number /10;
}

return Reverse;
}```

C reverse an integer output

``````Please Enter any number to Reverse
12345
Reverse of entered number is = 54321``````

Within this program to reverse a number, When the compiler reaches to Reverse_Integer (Number) line in the main() program then the compiler will immediately jump to below function:

int Reverse_Integer (int Number)

We already explained the code LOGIC in the above example. Please refer to C Program to Reverse an Integer Using While Loop Analysis. The last line ends with a return Reverse statement.

NOTE: If we create a function with Void, there is no need to return anything. However, if we declared a function with datatype (int, float, etc.), then we have to return something.

## C Program to Reverse a Number Using Recursion

This program to reverse a number, allows us to enter any positive integer. Then it will reverse a number using Recursion.

```/* C Program to Reverse a Number Using Functions */

#include <stdio.h>

int Reverse_Integer (int );

int main()
{
int Number, Reverse = 0;

printf("\nPlease Enter any number to Reverse\n");
scanf("%d", & Number);

Reverse = Reverse_Integer (Number);

printf("Reverse of entered number is = %d\n", Reverse);

return 0;
}

int Reverse_Integer (int Number)
{
static Reminder, Reverse = 0;

if (Number > 0)
{
Reminder = Number %10;
Reverse = Reverse *10+ Reminder;
Reverse_Integer (Number /10);
}
else
return Reverse;
}```
``````Please Enter any number to Reverse
3698
Reverse of entered number is = 8963``````

In this reverse a number program, When the compiler reaches to Reverse_Integer (Number) line in the main() program, the compiler will immediately jump to the below function:

int Reverse_Integer (int Number)

In this function, Reverse_Integer (Number /10); statement will help to call the function Recursively with the updated value. If you miss this statement, then after completing the first line, it will terminate. For example, Number = 459, then the output will be 9

Let’s see the If condition of this reverse a number in c

if (Number > 0) will check whether the number is greater than 0 or not. For Recursive functions, it is crucial to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like Infinite Loop).