## The logic to Reverse a Number Programmatically

This approach is completely based on mathematical calculations. First check for if number is negative or positive. If that number is negative convert that number to positive number by multiplying with -1. Then initialize reverse number variable with zero. Until the number is not zero perform these operations.

- Find remainder by dividing that number by 10.
- Multiply reverse number variable with 10 and add remainder to reverse number and update reverse number with new value.
- Divide that number with 10.

In order to understand the example below, you should have knowledge on the following topics:

## Reverse a Number in C Programming

```
#include<stdio.h>
int main(int argc, char const *argv[])
{
//input the number
int number;
printf("Reverse a number in c \nEnter a number to Reverse");
scanf("%d",&number);
int remainder=0;
int temp=number;
int reverse_number=0;
//check for negative number and convert that to positive first
int negative_flag=0;
if( number < 0 )
{
negative_flag = 1;
temp = temp * (-1);
}
while( temp>0 )
{
remainder = temp % 10;
reverse_number = ( 10*reverse_number) + remainder ;
temp=temp/10;
}
if( negative_flag )
{
reverse_number = (-1)*reverse_number;
}
printf( "Reverse of %d is %d\n",number,reverse_number );
return 0;
}
```

### Output

### Code Explanation

The program takes an integer as input to reverse it.

The number is checked it is negative or not, if it is negative then it is converted to a positive number.

While loop runs until the variable `temp > 0`

.

In each iteration of the while loop, the remainder is calculated and the reverse number is calculated using `reverse_number = ( 10*reverse_number) + remainder ;`

line.

In the end, if the input number is negative then the reverse number is converted to negative from positive.

## People also ask

### How do you reverse a number in C?

Initialize the reverse number variable with zero. Until the number, you want to reverse is not zero perform these operations.

1) Find the remainder by dividing that number by 10.

2) Multiply reverse number variable with 10 and add the remainder to reverse number and update reverse number with the new value.

3) Divide that number with 10.

### How do you reverse a number Program?

`while( temp>0 )`

{

remainder = temp % 10;

reverse_number = ( 10*reverse_number) + remainder ;

temp=temp/10;

}