Palindrome recursion python | We will develop a palindrome program in python using recursion. It will check if the given number is a palindrome number or not. If the Reverse of a number is equal to the same number then the number is called a palindrome number.
Example of palindrome number:-
69596 = 69596 So, 69596 is a palindrome number.
75849 != 94857 So, 75849 is not a palindrome number.
This program completely depends on the program to find the reverse of a number. After finding the reverse of a number, compare the result and the actual number if both are the same then the given number is a palindrome number else the number is not a palindrome number.
Prerequisite:- Python program to find reverse of a number
Palindrome Program in Python using Recursion
We will take integer numbers while declaring the variables. Then, call the function and check if the number is equal to the reverse number or not. Finally, the result will be displayed on the screen.
Program Description:- Write a Python program to check if the number is a palindrome or not using recursion
# palindrome number in python using recursion
reverse, base = 0, 1
def findReverse(n):
global reverse #function definition
global base #function definition
if(n > 0):
findReverse((int)(n/10))
reverse += (n % 10) * base
base *= 10
return reverse
# take inputs
num = 535
# calling function and display result
reverse = findReverse(num)
if(num == reverse):
print(num,'is a Palindrome')
else:
print(num,'is not a Palindrome')
Output:-
535 is a Palindrome
Palindrome Recursion Python
In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.
Program Description:- Write a program to check if the given number is a palindrome or not in Python using recursion
# palindrome number in python using recursion
reverse, base = 0, 1
def findReverse(n):
global reverse #function definition
global base #function definition
if(n > 0):
findReverse((int)(n/10))
reverse += (n % 10) * base
base *= 10
return reverse
# take inputs
num = int(input('Enter the number: '))
# calling function and display result
reverse = findReverse(num)
if(num == reverse):
print(num,'is a Palindrome')
else:
print(num,'is not a Palindrome')
Output for the input values test-case-1:-
Enter the number: 131
131 is a Palindrome
Output for the input values test-case-2:-
Enter the number: 789
789 is not a Palindrome
Output for the input values test-case-3:-
Enter the number: 3456543
3456543 is a Palindrome