Skip to content
Home » Programming » C program to reverse a number with explanation

C program to reverse a number with explanation

C Program to reverse a number

In this example, we will be learning how to write a program to reverse a number in C. We will use two methods to reverse a number; one being a basic method without using any of the lopping or conditional statements and another one is an iterative method. Most importantly, this example will help us to realize the use of loops in a program.

How will we reverse a number?

Let’s say we have a number with 4 digits 4321. First step is to separate individual digits of a number and then finally manipulate those individual digits to reverse a number.

Digits can be separated from individually by finding the remainder of the number after dividing by 10 and dividing it by 10. This step is repeated for all digits i.e. until the number finally becomes 0. This can be properly visualized using an example provided.

num = 4321
digit_1 = 4321 % 10 = 1
num = 4321 / 10 = 432
digit_2 = 432 % 10 = 2
num = 432 / 10 = 43
digit_3 = 43 % 10 = 3
num = 43 / 10 = 4
digit_4 = 4 % 10 = 4
num = 4 / 10 = 0

After that, we will be reversing digits since we have digits individually separated. For this step, we will be multiplying digits with an exponential of 10. The position a digit is to be at is the number we will multiply with i.e. if a digit is need to be placed at 100’s position then digit * 100.

Then Finally we will add all of them to find the reversed number.

reverse = digit_1 * 1000 + digit_2 * 100 + digit_3 * 10 + digit_4
i.e.
reverse = 1 * 1000 + 2 * 100 + 3 * 10 + 4 = 1234

Algorithm to reverse a number using basic method

Basic method simply means that we do not use loop or conditional statements to achieve the required output. Let’s take a look at algorithms to understand the working and flow of this program. This algorithm assumes that we have a 4 digit number.

Step 1: Start.
Step 2: Take a number as input in num.
Step 3: Make a copy of num for future reference. 
Step 4: digit_1 = num % 10;
Step 5: num = num / 10;
Step 6: digit_2 = num % 10;
Step 7: num = num / 10;
Step 8: digit_3 = num % 10;
Step 9: num = num / 10;
Step 10: digit_4 = num % 10;
Step 11: rev = digit_1 * 1000 + digit_2 * 100 + digit_3 * 10 + digit_4;
Step 12: Display original number and reversed number.
Step 13. End.

For a 5 digit number we add two more steps between step 10 and 11.

num = num / 10
digit_5 = num % 10;

As the number of digits increases we add those two steps for each digit.

C Program to reverse a number using basic method or without using loops

// considering first digit as the rightmost  digit

#include<stdio.h>

void main(){
    int num, digit_1, digit_2, digit_3, digit_4, rev, copy;
    printf("Enter a 4 digited number: ");
    scanf("%d", &num);
    copy = num;             //making copy of original number
    digit_1 = num % 10;
    num = num / 10;
    digit_2 = num % 10;
    num = num / 10;
    digit_3 = num % 10;
    num = num / 10;
    digit_4 = num % 10;
    rev = digit_1 * 1000 + digit_2 * 100 + digit_3 * 10 + digit_4;
    printf("\nOriginal number = %d", copy);
    printf("\nReverse number = %d", rev);
}

Output

Enter a 4 digited number: 4321

Original number = 4321
Reverse number = 1234

Explanation

  • At first, we took a 4 digit number as input in variable num.
printf("Enter a 4 digited number: ");
scanf("%d", &num);
  • Then, num is copied in variable copy so that we can store the original value for future reference.
copy = num;
  • Now here comes the part which we really need to understand. Individual digits are separated and stored in different variables; digit_1, digit_2, digit_3 and digit_4.
digit_1 = num % 10;

A number divided by 10 gives the remainder which is the first digit. And we have to eliminate the digit which is separated. This is done by dividing the number by 10.

num = num / 10;

This process is repeated until every digit of the number is separated.

  • After that, we have to add those individual digits in a specific manner to reverse the digits of that number.
rev = digit_1 * 1000 + digit_2 * 100 + digit_3 * 10 + digit_4;

Since digit_1 should be at thousand’s position we have to multiply it with 1000, digit_2 at hundred’s position so it is multiplied by 100 and so on. At last we add all those numbers and the result is a reversed number.

  • Finally, we print the original number and the reversed number.
printf("\nOriginal number = %d", copy);
printf("\nReverse number = %d", rev);

Limitation of Basic Method

As you can see that we are repeating two steps to separate every single digit.

digit_1 = num % 10;
num = num / 10;

As the number of digits increases it becomes hard to repeat them for every digit. Also, we need to know the no. of digits beforehand to execute the program. We cannot use the same program for different numbers with varying digit’s length.

Solution

One solution is to use loops. We can use the same program to reverse numbers with different numbers of digits. Also, it helps to reduce the number of code repetition.

Algorithm to reverse a number using iterative method

This algorithm will be used to show how a number can be reversed using an iterative method or using loops.

Step 1: Start.
Step 2: Take a number as input in num.
Step 3: Initialize rev = 0.
Step 4: Make a copy of num for future reference. 
Step 5: Repeat step 6 to step 8 until num > 0.
Step 6: digit = num % 10
Step 7: num = num / 10
Step 8: rev = rev * 10 + digit
Step 9: Display original number and reversed number.
Step 10: End.

In short,

Start,
    num = a number which is to be reversed
    rev = 0
    copy = num
    Repeat until num > 0
        digit = num % 10
        num = num / 10
        rev = rev * 10 + digit
    Display original number and reversed number
End.

C Program to reverse a number using iterative method or loops

// considering first digit as the right most digit

#include<stdio.h>

void main(){
    int num, digit, copy;
    int rev = 0;
    printf("Enter a 4 digited number: ");
    scanf("%d", &num);
    copy = num;             //making copy of original number
    while(num > 0){
        digit = num % 10;
        num = num / 10;
        rev = rev * 10 + digit;
    }
    printf("\nOriginal number = %d", copy);
    printf("\nReverse number = %d", rev);
}

Output

Enter a 4 digited number: 12345

Original number = 12345
Reverse number = 54321

Explanation

This program and the program using basic methods are mostly the same. The most important difference is how we separate digits. So, let’s explain that part only.

Here at first we check if the number is greater than 0 or not. If yes, we will execute main logic to separate digits. num greater than 0 means that there are still digits left in the num.

while(num > 0){
    . . .
}

Then, we separate a digit from the variable num and eliminate that digit from the number.

digit = num % 10;
num = num / 10;

After that, we multiply variable rev by 10 and add digit to rev.

rev = rev * 10 + digit;

More Examples

C Program to find the factorial of a number
C program to find the average/mean of N numbers

Leave a Reply

Your email address will not be published. Required fields are marked *