Python Program to Reverse Number Using Recursive Function
In this Python program, we read number from user and then pass this number to recursive function reverse()
. Here function reverse()
is recursive because it call itself.
This Python program finds reverse of a given integer number using recursion.
Python Source Code
# Reverse a number using recursion
def reverse(n, r):
if n==0:
return r
else:
return reverse(n//10, r*10 + n%10)
# Read number
number = int(input("Enter number: "))
# Function call
reversed_number = reverse(number,0)
# Display output
print("Reverse of %d is %d" %(number, reversed_number))
Output
Enter number: 374 Reverse of 374 is 473