Home » Palindrome Program in Python using Recursion

Palindrome Program in Python using Recursion

Learn about Palindrome Program in Python using Recursion in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Palindrome Program in Python using Recursion

A palindrome number is one whose digits remain the same when they are reversed. The below python program checks whether the number given by the user is a palindrome or not using recursion. Refer the comments in the program for better understanding.

Program:

# Python program to check if number is Palindrome using recursion
#initialize variables
reverse, base = 0, 1

def findReverse(n):
    global reverse  
    global base 
    if(n > 0):
        findReverse((int)(n/10))
        reverse += (n % 10) * base
        base *= 10
    return reverse

# input number
num = int(input('Enter the number: '))

# function call
reverse = findReverse(num)
if(num == reverse):
  print(num,'is a Palindrome')
else:
  print(num,'is not a Palindrome')

Output:

Enter the number: 12421
12421 is a Palindrome

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Palindrome Program in Python