 # Python Program to Reverse Numbers

Reverse of a number in python; In this tutorial, you will learn how to reverse number in python program using while loop and recursion function.

## Python Program to Reverse Numbers

• Python Program to Reverse a Number using While loop
• Python Program to Reverse a Number Using Recursion

### 1: Python Program to Reverse a Number using While loop

• Take input from the user in program.
• Define 0 to the variable “test_num”.
• Check whether the given number is greater than zero using while loop.
• If yes, find the remainder by performing modulus of 10 with the input.
• Multiply test_num by 10 and add the remainder to it, store the answer in rev.
• Get the quotient of the input.
• The loop will repeat until the number is reversed.
• Print Reverse a number
``` # Get input from the user
num = int(input("Please enter any number: "))

# Initiate value to null
test_num = 0

# iterate number using while loop
while(num>0):
#Logic to reverse a number
remainder = num % 10
test_num = (test_num * 10) + remainder
num = num//10

# print the result
print("The reverse number is : {}".format(test_num))
```

Output

```Please enter any number:  123
The reverse number is : 321 ```

### 2: Python Program to Reverse a Number using Using Recursion

• Take input from the user in program.
• Define 0 to the variable “output”.
• Define function and Implement logic to reverse a number
• Check whether the given number is greater than zero using while loop.
• If yes, find the remainder by performing modulus of 10 with the input.
• Multiply test_num by 10 and add the remainder to it, store the answer in rev.
• Get the quotient of the input.
• The loop will repeat until the number is reversed.
• Call Function, which is define above.
• Print Reverse a number.
```# Get input from user
Num = int(input("Please Enter any Number: "))

Output = 0
def revNum(Num):
global Output
if(Num > 0):
Reminder = Num %10
Output = (Output *10) + Reminder
revNum(Num //10)
return Output

Output = revNum(Num)

print("Reverse of entered number is = %d" %Output)
```

Output

```Please Enter any Number:  456
Reverse of entered number is = 654 ```

## Recommended Python Programs 