# Python Program to Reverse a Number

Write a program in python to reverse the digits of a number.

**Example:**Input: number = 1234 Output: reverse number = 4321

## Reverse a Number in Python

```
# Python program to reverse a number
def reverse(number):
reverse_number = 0
while(number>0):
r = number % 10;
reverse_number = reverse_number*10 + r
number = number / 10
return reverse_number;
# Driver Program
number = 1234
print("Given Number: ")
print(number)
print("Reversed Number: ")
print(reverse(number))
```

### Output:

Given Number: 1234 Reversed Number: 4321

### Explanation:

We have defined a function reverse which takes one argument**number**.**reverse_number**variable is used to hold the reverse the number, initialized to 0. Now we get the last digit of original**number**by using modulo operation. We use the formula`reverse_number = reverse_number*10 + r`

where r is the remainder i.e. the last digits of original **number**. Then divide the original number. Continue this process until original**number**becomes 0.Input: number = 1234 Pass this to reverse() function as reverse(number) Set reverse number = 0 Get last digit r = 1234 % 10 = 4 reverse number = reverse number * 10 + r So reverse number = 0 * 10 + 4 = 4 Divide original number by 10 number = number / 10 number = 1234 / 10 = 123 Again obtain as above, so r = 3 Again calculate reverse number as above, so reverse number = 4 * 10 + 3 = 43 Again divide original number by 10, so number = 12 (after dividing) Again find r, r = 2 calculate reverse number = 43 * 10 + 2 = 432 Divide number by 10 So number = 1 Again find r, r = 1 calculate reverse number = 432 * 10 + 1 = 4321 Divide number by 10 So number = 1 / 10 = 0 So stop here and return the reverse number Output: reverse number = 4321You can also do this without using function, below is the code for that

```
# Python program to reverse a number
# set or accept number to reverse
number = 1234
# store original number
original_number = number
reverse_number = 0
while(number>0):
r = number % 10;
reverse_number = reverse_number*10 + r
number = number / 10
print("Given Number: ")
print(original_number)
print("Reversed Number: ")
print(reverse_number)
```