The problem is, we are given a number and we need to write a python program to check whether the given number is a palindrome or not.

Palindrome Number: A number is said to be a palindrome if its reverse is equal to itself.

Palindrome number in Python

Steps to Check Palindrome Number in Python

  • Take a number as input num.
  • Find the reverse of the number rev
  • Compare rev with the original number.
  • If both are equal then the number is Palindrome else it is not.

Check Whether a number is a Palindrome in Python

print("Enter a Number \n")
num = int(input())
rev = 0

#Copying the original number
temp = num

#Finding Reverse
while temp > 0:
    rev = (rev*10) + (temp %10);
    temp = temp//10

#Comparing reverse with original number
if rev == num :
    print("Palindrome \n")
else:
    print("Not Palindrome")

Output

Python program to check Palindrome

Find all the Magic Numbers in the interval in Python

To print all palindrome numbers in the given range we will run the above code for each number. Therefore we will write the code which verifies the palindrome number into a function and will call that function from the loop.

def isPalindrome(num):
  rev = 0

  #Copying the original number
  temp = num

  #Finding Reverse
  while temp > 0:
      rev = (rev*10) + (temp %10);
      temp = temp//10

  return rev == num


low = int(input("Enter lower interval value \n"))
up = int(input("Enter upper interval value \n"))
 
print("Palindeome numbers between {x} and {y} are".format(x=low,y=up))
 
for num in range(low,up+1):
    if isPalindrome(num):
      print(num)

Output

Palindrome numbers between 1 to 100

Comment below if you have any suggestions.

Leave a Reply