In this program, we will check whether a number is a palindrome or not.
Steps to solve the program
- Take a number as input through the user and create a variable rev and assign its value equal to 0.
- Use a while loop to find the reverse of a number.
- While num is greater than 0 divide the number by 10 using % to find the remainder.
- Multiply the rev by 10 and add the remainder to it.
- Now divide the number by 10 using //.
- If the reversed number is equal to the input number then it is a palindrome.
num=n=int(input("Enter a number: "))
rev = 0
while n>0:
r = n%10
rev = (rev*10) + r
n = n//10
print("Reverse number: ",rev)
if num == rev:
print("It is a palindrome")
else:
print("It is not a palindrome")
Output :
Enter a number: 121
Reverse number: 121
It is a palindrome