Reverse a Number in Python Using Recursion

(Last Updated On: 28/12/2022)

Python Program to Reverse a Number Using Recursion

# Reverse a Number in python Using recursion
def reverse(n, r):
	if n==0:
    	return r
    	return reverse(n//10, r*10 + n%10)

# we are taking a number from user as input
# entered value will be converted to int from string  
number = int(input("Enter number: "))

# we read number from user and then pass this number to recursive function reverse()
reversed_number = reverse(number,0)

# Display output
print("Reverse of %d is %d" %(number, reversed_number))


Enter number: 123456
Reverse of 123456 is 654321

Leave a Reply

Your email address will not be published. Required fields are marked *