Python - Palindrome Number or not
Problem Statement
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
- 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.
- At first a copy of the entered value is stored in temp.
- After that the remaining statements written within else block are executed.
num | temp!=0 | rem=temp%10; | rem | rev=rev*10+rem; | rev | temp=temp/10; | temp | Comment |
---|---|---|---|---|---|---|---|---|
151 | - | - | - | - | 0 | - | 151 | Initial Values |
151 | 151!=0 ↓ True | rem=151%10; | 1 | rev=0*10+1; | 1 | temp=151/10; | 15 | Iteration 1 |
151 | 15!=0 ↓ True | rem=15%10; | 5 | rev=1*10+5; | 15 | temp=15/10; | 1 | Iteration 2 |
151 | 1!=0 ↓ True | rem=1%10; | 1 | rev=15*10+1; | 151 | temp=1/10; | 0 | Iteration 3 |
151 | 0!=0 ↓ False | - | - | - | - | - | - | While loop stops working as temp!=0 is evaluated to False |
- 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.