You are currently viewing How to check if a number is Palindrome number in Python?

How to check if a number is Palindrome number in Python?

In the previous article, we have seen how to reverse a number. In this article, we will check if a number is a Palindrome or not. We will take inputs from the user and will analyze it for its palindromic nature.

Before we jump directly to the program, let us see, what is a palindromic number? A palindromic number is a number that is the same when written forwards or backward. For example, 101, 202, 333 etc. All the single-digit numbers are palindromic in nature. So the numbers:  0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all are palindromic.

Program to Check if a number is a palindrome

In this program, we will ask the user to input a number and then we will reverse it and then we will compare both the numbers, to check if they are same. if they are same then they are palindromic if they are not same then they are not.

Code:

number=input("Enter the number ") // Taking input form user
number2=int(number)
reverse=0
for i in range(len(number)):
lastvalue = int(number) % 10
reverse = (reverse * 10) + lastvalue
number = int(number) / 10

if(number2==reverse):
print("number is palindrome number")
else:
print("Number is not palindrome number")

Output:

Leave a Reply