Reverse a Number in C

Introduction

Before, writing a c program to reverse a number, let’s see the expected output first.

Enter a Number
12345
The reverse of 12345 is 54321

In the above output, we have read one number and then display the reverse of that number. 12345 is just a example. User can enter any number of any length. So, I am writing here a program to reverse a number in c and then I will explain this program line by line.

Also Read: Perfect Number in C Programming using All Loops

C Program to Reverse a Number

#include <stdio.h>
int main()
{
    int rem,rev=0,n,an;
    printf("Enter any Number\n");
    scanf("%d",&n);
    an=n;
    while(n>0)
    {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }
    printf("Reverse of %d is %d",an,rev);
    return 0;
}

Explanation

int rem, rev=0, n,an;

In the above c program, I have declared 4 variables and they all have the same type i.e. int. Here, rem means remainder. In this variable, I will store the remainder of the number. The rev=0 means reverse. Out output will be stored in this variable. The variable n means the number that we have to reverse and the variable an means actual number.

Also Read: Factorial Program in C Programming

scanf(“%d”,&n);

This is the input statement. Here, we are reading a number from the console and storing that number in the variable ‘n’. Suppose, the user has entered 12345 then the value of n becomes 12345 i.e. n=12345.

an=n;

This line is optional. Here, an means the actual number. I am assigning the value of n to an. Why? In further processing part, the value of n will be changed and it will become 0. So, while displaying our output, I need this value. Therefore, I am just copying this value to the variable an.

The while loop in C to Reverse a Number

    while(n>0)
    {
        rem=n%10;
        rev=rev*10+rem;
        n=n/10;
    }

In this while loop, I am checking the condition of n>0. If it is greater than 0, then all these three statements will be repeated, otherwise, this part will be skipped.

Iteration 1 when n = 12345

Now, the value of n is 12345 which is greater than 0. Therefore, loop will be executed.

Also Read: LCM of Two Numbers in C Programming

In the loop body, first statement is rem=n%10; What is the use of this statement? When we divide any number by 10 then the last digit of that number is remainder. In this case, we are dividing 12345 by 10, that means the value of rem becomes 5. How? Because we are using modulo division operator (%) here and it returns the remainder after the division operation. This is the most important statement to reverse a number in c programming.

Next statement in this loop statement rev=rev*10+rem;. In the first statement, we got the last digit as a remainder. Now, we have to make this last digit as the first digit. So, this statement will help us. Initially, we have made the value of rev = 0. So, when we execute this statement, then the value of rev becomes 5. You can put the values of rev and rem in the above expression, then the value of rev becomes 5.

Also Read: GCD of Two Numbers in C Programming

Now, we have got our first digit i.e. 5. So, we will have to find our another digit i.e. 4. How could we do this? Same procedure we will have to follow. But, we will have to make one change and that change is, make the value of n = 1234. Because we already got our first digit i.e. 5. So, how can we do this? The third statement in the above while loop will help us and that statement is n=n/10;

When we divide any number by 10 then we will get quotient of that number excluding last digit. For example, in this case, n / 10 means 12345/10 and the answer is 1234. So, this value is stored in n. Now the new value of n=1234.

This is the first iteration of while loop. After every iteration, the value of n becomes less than 1 digit.

Iteration 2 when n = 1234

Now, we are in the second iteration. Again, the value of n will be compared that whether it is greater than 0 or not. In this iteration, condition is true. So, again this body of while loop will be executed.

In this iteration, the values are : rem=4, rev=54 and n=123.

Iteration 3 when n = 123

Now we are in the third iteration. You can see, the value n becomes lesser and lesser by one digit.

In this iteration, the values are : rem=3, rev=543 and n=12.

Iteration 4 when n = 12

In this iteration, the values are : rem=2, rev=5432 and n=1.

Iteration 5 when n = 1

In this iteration, the values are : rem=1, rev=54321 and n=0.

Now, the value of n becomes 0, therefore, the while loop condition becomes false and therefore, this loop will be executed. After executing this loop, we will go to the next statement where we are displaying our output.

Also Read: C Program to Remove Zeros from a number

printf(“Reverse of %d is %d”,an,rev);

This is print() statement. This statement will display the output to the console. The output is Reverse of 12345 is 54321. Because the value of an is 12345 and that of rev is 54321.

So, this is the c program to reverse a number. I hope you have understood this program. This program can be done in many ways. I have used while loop in this c program, you can use any loop like, for or do while loop.

Also Read: Programs on Arrays in C

You can write the same program using function also. If you know Hindi language, then I have already made a video on this topic i.e reverse a number in c. You can watch this video and I hope you will like this video. So, here is the link.

Thank you.

Leave a Comment