ComputerShikshak.com

Python - Palindrome Number or not

Problem Statement

Write a program in Python to check whether an integer number entered by the user is a Palindrome number or not.

Brief Description

According to the problem statement we have to write a program in Python that will take an integer value as input from the user through the keyboard and will check whether it is palindrome or not.

A number is palindrome if it is equal to the number obtained by reversing the digits of that number.

For example, if the user enters 151 as input, the program will generate an output indicating that 151 is palindrome. This is because, if a number is constructed from the digits of 151 by reversing the digits of it then 151 is obtained as a result which is equal to the entered number.

But, if the user enters 157 as input, the program will generate an output indicating that 157 is not palindrome. This is because, if a number is constructed from the digits of 157 by reversing the digits of it then 751 is obtained as a result which is not equal to the entered number.

Program

				
					num=int(input("Enter a number: "))
rev=0
if num<0:
    print("Please enter a value greater than or equal to zero")
else:
    temp=num
    while temp!=0:
        rem=temp%10
        rev=rev*10+rem
        temp=temp//10    #OR temp//=10
    if num==rev:
        print(num,"is a palindrome number")
    else:
        print(num,"is not a palindrome number")

				
			

Output 1

Output 2

Explanation

In this program:
  • 4 variables namely num, temp, rev and rem are used.
  • Initially 0 is stored in rev.
  • The integer value entered by the user is stored in num.
  • If a negative number is entered by the user, the condition num<0 is evaluated to True and the message “Please enter a value greater than or equal to zero” is displayed and the program is terminated.
  • If a number which is greater than or equal to zero is entered, the condition num<0 is evaluated to False and control goes to the else block and the statements written within else block are executed.
Within else block:
  • At first a copy of the entered value is stored in temp.
  • After that the remaining statements written within else block are executed.
The step-by-step working of the while loop (when num and temp both contains 151) is shown in the table given below:
numtemp!=0rem=temp%10;remrev=rev*10+rem;revtemp=temp/10;tempComment
151----0-151Initial Values
151151!=0

True
rem=151%10;1rev=0*10+1;1temp=151/10;15Iteration 1
15115!=0

True
rem=15%10;5rev=1*10+5;15temp=15/10;1Iteration 2
1511!=0

True
rem=1%10;1rev=15*10+1;151temp=1/10;0Iteration 3
1510!=0

False
------While
loop
stops
working
as
temp!=0
is
evaluated
to
False
While executing the while loop:
  • At first value of temp is checked to see whether it is 0 or not.
  • The condition temp!=0 will produce either True or False after evaluation.
    • If it is True, the statements written within while loop are executed and the loop continues to iterate till the condition is True.
    • If it is False then the statements written within while loop are not executed and the control goes to the statement written immediately after the loop.
  • Within while loop, at first the last digit is extracted from the value stored in temp by performing modulo division operation using the expression temp%10. Thus the expression temp%10 will give the remainder after integer division which is the last digit of the value stored in temp and the result is stored in rem

  • After that the value stored in rev is multiplied with 10 and the value obtained in rem is added with it and the result is stored in the same variable i.e. rev. The expression rev=rev*10+rem; is used to construct a number from individual digits.

  • The value stored in temp is divided by 10 to remove the last digit of the value stored in it and the result is stored in the same variable i.e. temp.

Thus the above process is used to reverse the digits present in the given number.

The final result is stored in rev. 

Finally it is checked to see whether the value stored in num is equal to the value stored in rev. If it is then we can conclude that the value stored in num is a palindrome number otherwise not and an appropriate message is displayed on the output screen.

Note:-
If we do not store a copy of the value entered by the user in temp and perform the whole computation using the value stored in num then while displaying the final result we would not be able to display the entered value along with the result as value of num would become 0 after computation(exactly like the value of temp).
Share this page on
Scroll to Top