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.