;

Python Program to Check Whether the Number is Palindrome or Not using Recursion


Tutorialsrack 23/04/2020 Python

In this Python program, we will learn how to check if a given number is a palindrome or not using recursion. 

What is Palindrome?

A palindrome is a word, number, phrase, or other sequences of characters that reads the same backward as forward, such as civic or rotator or the number 14241. 

A number is a palindrome when we reverse the number and the reversed number is equal to the original number.

Here is the code of the program to check if the given number is palindrome or not using recursion.

Python Program to Check Whether the Number is Palindrome or Not using Recursion
# Python Program to Check Whether the Number is Palindrome or Not using Recursion

# Define a Recursive Function
def revNum(n, temp):  
    # base case  
    if (n == 0):  
        return temp;  
  
    # stores the reverse of a number  
    temp = (temp * 10) + (n % 10);  
    return revNum(n // 10, temp);  
  

# To Take Input From the USer
num = int(input("Enter the Number: "));  
temp = revNum(num, 0);  
  
if (temp == num):  
    print(num, "is a Palindrome");  
else: 
    print(num, "is not a Palindrome"); 
Output

Enter the Number: 525

525 is a Palindrome

 

Enter the Number: 533

533 is not a Palindrome


Related Posts



Comments

Recent Posts
Tags