Palindrome Number in Python

Previously we have developed many python programs based on Prime numbers. Now in this post, we will develop a palindrome number in python. It will check the given number is a palindrome number or not.

Palindrome number:- If the Reverse of a number is equal to the same number then the number is called a palindrome number.

Example of palindrome number:-
5225 = 5225 So, 5225 is a palindrome number.
123 = 321 So, 123 is not a palindrome number.

This program completely depends on the program to find the reverse of a number. After finding the reverse of a number compare the result and the actual number if both are the same then the given number is a palindrome number else the number is not a palindrome number.

Prerequisite:- Python program to find reverse of a number

Palindrome Program in Python

This python program using the while loop to check number is palindrome or not. We will take integer numbers while declaring the variables. Then, find the reverse of a number using the while loop and check number is equal to the reverse number or not using the if-else statement. Finally, the result will be displayed on the screen.

# Python program to check if number is Palindrome

# take inputs
num = int(input('Enter the number : '))

# calculate reverse of number
reverse = 0
number = num
while(num != 0):
  remainder = num % 10
  reverse = reverse * 10 + remainder
  num = int(num / 10)

# compare reverse to original number
if(number == reverse):
  print(number,'is a Palindrome')
else:
  print(number,'is not a Palindrome')

Output for the different input values:-

Enter the number: 121
121 is a Palindrome

Enter the number: 123
123 is not a Palindrome

Palindrome Program in Python using Recursion

We can also use the recursion technique to check if a number is palindrome or not in Python. A technique of defining the method/function that contains a call to itself is called recursion.

# Python program to check if number is Palindrome using recursion

reverse, base = 0, 1
def findReverse(n):
    global reverse  #function definition
    global base   #function definition
    if(n > 0):
        findReverse((int)(n/10))
        reverse += (n % 10) * base
        base *= 10
    return reverse

# take inputs
num = int(input('Enter the number: '))

# calling function and display result
reverse = findReverse(num)
if(num == reverse):
  print(num,'is a Palindrome')
else:
  print(num,'is not a Palindrome')

Output:-

Enter the number: 12345
12345 is not a Palindrome

Python Program using Slicing

We read a number and reverse a number using slice operations. We will convert the integer number to string using str() and then, calculate the reverse of a number using the slicing operation.

Syntax of slicing operation:- str(num) [::-1]

# Python program to check if number is Palindrome using slicing

# take inputs
num = int(input('Enter the number: '))

# calculate reverse of number
reverse = int(str(num)[::-1])

# compare reverse to original number
if(num == reverse):
    print(num,'is a Palindrome')
else:
    print(num,'is not a Palindrome')

Output:-

Enter the number: 7542457
7542457 is a Palindrome

We can also read input as string and then simply check for palindrome.

num = input('Enter the number: ')
if(num == num[::-1]):
    print(num,'is a Palindrome')
else:
    print(num,'is not a Palindrome')

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *