## Monday, June 28, 2021

Program to reverse a number with full Explanation

Here we will discuss, how we reverse a number.

When someone asks you to reverse number 4586, then you easily say that it will be 6854. But if someone asks you to write a program to reverse a number, then it will be little bit harder.

So here we will learn with the full explanation that how we will write a program to reverse a number.

Let’s start,

We have a plus point in C that when we divide a number it simply gives us dividend, not a remainder or a decimal value.

For example,

458/10= 45 (according to the C )

So we will use it.

To reverse a number we have to first take its last digit.

Like we have a number 4567

To get 7, we will

4567-(4567/10)*10=7 (as per C language)

Then

456-(456/10)*10=6

Similarly

45-(45/10)*10=5  and so on.

Now we will make this single-digit number into a number. So, we will write a reverse program.

Int num,rev=0,rem;

while(num!=0){

rem=num%10;

rev=rev*10+rem;

This code will help us to reverse the number like

Rev =0

Therefore, when the remainder will be the last digit then it will be

Rev=0*10 + rem

After this step, we will write

num=num/10;

This statement will convert 3 digit number into 2 digit number and then into 1 digit number.

For example,

348/10=34 (According to C)

34/10=3 and so on.

And as we write a loop program. So it will keep calculating until the number becomes reverse.

The full program to reverse a number is as follows:

#include<stdio.h>

int main()

{

int n,r=0, rem;

printf("Enter the value of a: ");

scanf("%d", &n);

while(n!=0)

{

rem=n%10;

r=r*10+rem;

n/=10;

}

printf("Reversed Number is %d",r);

return 0;

}

That’s all for the program of reverse number.

For more such blogs keep visiting messwithage.