In this program, we are going to check whether the given number is palindrome or not by using python.
Before we start our program, there is a need to know what is a palindrome?
The palindrome is set to be a number whose reverse is said to be equal. In this program, we will be using if and while statement. If the given condition placed in “if ” statement is true then it is a palindrome else it is not.
For Example:
Input: 121
Output: It is a palindrome
Algorithm:
- Take a number from the user and store in a variable.
- Transfer the value of number to the temporary variable.
- With the help of while loop we reverse the number and store it in the variable.
- We will check the reverse number with the temporary variable.
- If both are equal then it is a palindrome.
- Else it is not a palindrome.
Code:
#program to check given number is palindrome or not
n= int(input(“Enter the number: ”))
temp=n //storing the number in temporary variable
rev=0
while(n>0):
digit=n%10
rev=rev*10+digit //reversing the digit and storing it in a variable
n=n//10
if (temp==rev) : //comparing temp variable and rev variable
print(“It is a palindrome ”)
else:
print(“It is not a palindrome ”)
Output:
Enter the number: 191
It is a palindrome
Report Error/ Suggestion