Python Program To Check A Number Or String Is Palindrome

(Last Updated On: 04/11/2022)

A palindrome is a string that is the same read forward or backward.

For example, "1221" is the same in the forward or reverse direction. Another example is “MOM”, “DAD”, which literally means, an irritable fear of palindromes.

# Python Program To Check A Number Is Palindrome or not

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

# integer is stored in another temporary variable
temp = n
rn = 0

while n>0:
    r = n % 10
    rn = rn * 10 + r
    n = n // 10
    
if rn == temp:
    #If both are equal, the number is a palindrome.
    print("The number is Palindrome") 
else:
    #If both aren’t equal, the number isn’t a palindrome.
     print("The number is not Palindrome")

Output:

Case 1:

If the number is 1221

Enter any number: 1221
The number is Palindrome
Case 2:

If the number is 1451

Enter any number: 1451
The number is not Palindrome

Leave a Reply

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