In this article, you will learn to write a C program to reverse a number. Reversing a number in C programming means changing all the digits of a number to bring the digit at the last position to the first position and vice-versa. Suppose you take the number 1234; the reverse is 4321. There are multiple ways to write a C program to reverse a number, and we will be looking at all of them in this article. But before delving deep into writing the program itself, letâ€™s first understand the algorithm and logic behind it.

## Understanding the Algorithm of a C Program to Reverse a Number

In real life, we can simply flip the number to find its reverse. But how do we build the logic to make that happen through a program? Letâ€™s find out.

- We will declare two int variables: â€˜Numberâ€™ store the number to be reversed, and â€˜Reversed_Number,â€™ initialized to be 0, will hold the resultant reversed number.
- Now, we will use the formula Reversed_Number = Reversed_Number*10 + Number%10.
- This formula divides the number by 10 and stores the remainder multiplied by 10 as the value of the â€˜Reversed_Number.â€™
- After applying the formula, we will divide the number by 10 and update its value to eliminate the last digit that is already reversed.
- We will repeat this until the value of the number becomes less than 0.
- When the number goes below 0, the â€˜Reversed_Numberâ€™ variable will have the result.

## Flowchart of C Program to Reverse a Number

The algorithm and logic mentioned in the previous section can be demonstrated using the following flowchart:

Using the above flowchart, we can create a C program to reverse a number using various methods. Now we will look at those multiple methods and reverse a number.

## C Program to Reverse a Number Using While Loop

The first method that we will use to reverse a number in C is through a while loop. We will create and use the while loop to continue processing the formula until the value of the number becomes 0. Hereâ€™s the implementation of the program using the while loop.

### Example

#include <stdio.h>

int main(){

Â Â Â Â int Num, rev_Num = 0, remainder;

Â Â Â Â printf("Enter the number to reverse: ");

Â Â Â Â scanf("%d", &Num);Â Â Â Â

Â Â Â Â while (Num != 0){

Â Â Â Â Â Â Â Â remainder = Num % 10;

Â Â Â Â Â Â Â Â rev_Num = rev_Num * 10 + remainder;

Â Â Â Â Â Â Â Â Num = Num/10;

Â Â Â Â }Â Â Â Â

Â Â Â Â printf("The reversed number is: %d", rev_Num);

Â Â Â Â return 0;

}

#### Output:

## C Program to Reverse a Number Using Recursion

You can also write a C program to reverse a number using recursion. The flowchart and algorithm remain the same. However, due to recursion, the function will call itself until the condition becomes false. Hence, we wonâ€™t have to use any loop in this example.

### Example

#include<stdio.h>

int main(){

Â Â Â Â int Num,rev_Num;

Â Â Â Â printf("Enter the number to reverse: ");

Â Â Â Â scanf("%d",&Num);Â Â Â Â

Â Â Â Â //Calling our function that will repeat itself

Â Â Â Â rev_Num=rev_Func(Num);

Â Â Â Â printf("\nThe reversed number is :%d",rev_Num);

Â Â Â Â return 0;

}

int sum=0,remainder;

rev_Func(int Num){Â Â Â Â

Â Â Â Â if(Num){

Â Â Â Â Â Â Â Â remainder=Num%10;

Â Â Â Â Â Â Â Â sum=sum*10+remainder;

Â Â Â Â Â Â Â Â rev_Func(Num/10);

Â Â Â Â }

Â Â Â Â else

Â Â Â Â Â Â Â Â return sum;

Â Â Â Â return sum;

}

#### Output:

## C Program to Reverse a Number Using For Loop

For this example, instead of using a while loop, we will use a for loop in C to execute the formula until the condition becomes false. The following code demonstrates a C program to reverse a number using a for loop.

### Example

#include<stdio.h>

void main(){

Â Â Â Â int Num,rev_Num=0,remainder,a;Â

Â Â Â Â printf("Enter the number to reverse: ");

Â Â Â Â scanf("%d",&Num);

Â Â Â Â a=Num;

Â Â Â Â for(;Num>0;){

Â Â Â Â Â Â Â Â remainder=Num%10;

Â Â Â Â Â Â Â Â rev_Num=rev_Num*10+remainder;

Â Â Â Â Â Â Â Â Num=Num/10;

Â Â Â Â }

Â Â Â Â printf("Reverse of %d is %d",a,rev_Num);

}

#### Output:

## C Program to Reverse a Number Using Function

Here, we will create a function in the main method and use the algorithm inside it to calculate the reverse of the number entered by the user.

### Example

#include <stdio.h>

int rev_Int(int);

int main(){

Â Â Â Â int Num, Rev = 0;Â Â

Â Â Â Â printf("\nEnter the number to reverse: ");

Â Â Â Â scanf("%d", &Num);Â Â

Â Â Â Â Rev = rev_Int (Num);

Â Â Â Â printf("Reverse of %d is = %d\n", Num, Rev);

Â Â Â Â return 0;

}

int rev_Int (int Num){

Â Â Â Â int remainder, Rev = 0;Â Â Â Â

Â Â Â Â while (Num > 0){

Â Â Â Â Â Â Â Â remainder = Num %10;

Â Â Â Â Â Â Â Â Rev = Rev *10+ remainder;

Â Â Â Â Â Â Â Â Num = Num /10;

Â Â Â Â }Â Â Â Â

Â Â Â Â return Rev;

}

#### Output:

Advance your career as a MEAN stack developer with theÂ Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

## Wrapping It Up

In this article, you have learned everything you needed to know about a C program to reverse a number. You have also seen examples of how to reverse a number in C using different methods and loops. While the methods change, the logic behind the calculations remains the same as always. You can now try using the logic in different ways and create complex programs requiring reversing a number.

Reversing a number is just a basic concept of C programming. If you want to learn more about such fundamentals, you can sign up on our SkillUp platform. The platform offers numerous free courses in multiple programming languages to help you brush up and improve your coding skills.

In todayâ€™s world, mastering multiple programming skills and languages has become vital as the demand for full-stack developers keeps increasing. Our Full-Stack Web Development Certification Course is made for just that. The course familiarizes you and helps you learn around 30 different programming tools and multiple languages. It also provides you with a certification that gives credibility to your skills. So why wait? Go ahead and opt for the certification course.