Python program to check number is palindrome or not

Python program to check whether the given number is palindrome or not

Write a Python program to accept a number from the user, and check whether the given number is palindrome or not.

Video Tutorial

What is the meaning of a palindrome Number..?

A number is said to be palindrome if the number is equal to its reversed number. For example, 121 is a palindrome as the reverse of 121 is 121.

Now, first, we need to find the reverse of the given number. Then we need to check whether the reversed number is equal to the original number. If the reversed number is equal to the original number then the number is palindrome otherwise the given number is not a palindrome.

Logic to find the reverse of a given number:

Let the user has entered the number 123. Then the procedure to find the reverse of a number is given below.

num = 123 rev = 0

Iteration 1

rem = num % 10 = 123 % 10 = 3

See also  Solution to Python Application Programming Question Paper July 2019

rev = rev * 10 + rem = 3

num = num //10 = 123 // 10 = 12

Iteration 2

rem = num % 10 = 12 % 10 = 2

rev = rev * 10 + rem = 30 + 2 = 32

num = num //10 = 12 // 10 = 1

Iteration 3

rem = num % 10 = 1 % 10 = 1

rev = rev * 10 + rem = 320 + 1 = 321

num = num //10 = 1 // 10 = 0

As the value of num reaches 0, we need to stop here. The reverse number is 321.

The original number is 123 and its reverse is 321. Hence 123 is not a palindrome.

Source code of Python program to check whether number is palindrome or not

num = int (input("Enter the number:"))
org = num
rev = 0

while num > 0:
    rem = num % 10
    rev = rev * 10 + rem
    num = num // 10
    
print ("The reverse of ", org, "is ", rev)
if rev == org:
    print ("The number ", org, "is Palindrome")
else:
    print ("The number ", org, "is not a Palindrome")

Output:

Case 1:

Enter the number: 121

The reverse of 121 is 121

See also  Solution to Python Application Programming Question Paper July 2018

The number 121 is Palindrome

Case 2:

Enter the number: 123

The reverse of 123 is 321

The number 123 is not a Palindrome

Summary:

This tutorial discusses how to write a Python program to check whether the number is palindrome or not. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

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