C Program to Reverse a Number

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.

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

Flowchart of C Program to Reverse a Number

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

C_Program_Reverse_Number_1.

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_Reverse_Number_2

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_Reverse_Number_3

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_Reverse_Number_4

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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:

C_Program_Reverse_Number_5.

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.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.