Palindrome Number in Python using Function

Previously we have developed palindrome numbers in python and palindrome strings in python. Now in this post, we will check the palindrome number in python using function. If the Reverse of a number is equal to the same number then the number is called a palindrome number.

Example of palindrome number:-
121 = 121 So, 121 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 using Function

We will take integer numbers while declaring the variables. Then, call the function and check if the number is equal to the reverse number or not using the if-else statement. Finally, the result will be displayed on the screen.

Program Description:- Python program to check palindrome number using functions

# Palindrome number in python using function

def isPalindrome(n):  #user-defined function
    # calculate reverse of number
    reverse = 0
    reminder = 0
    while(n != 0):
        remainder = n % 10
        reverse = reverse * 10 + remainder
        n = int(n / 10)
    return reverse

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

Output:-

55 is a Palindrome

Palindrome Number in Python using Function

In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.

Program Description:- Write a program to check if the given number is a palindrome or not in Python using function

# Palindrome number in python using function

def isPalindrome(n):  #user-defined function
    # calculate reverse of number
    reverse = 0
    reminder = 0
    while(n != 0):
        remainder = n % 10
        reverse = reverse * 10 + remainder
        n = int(n / 10)
    return reverse

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

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

Output for the input values test-case-1:-

Enter the number: 454
454 is a Palindrome

Output for the input values test-case-2:-

Enter the number: 4546
4546 is not a Palindrome

Output for the input values test-case-3:-

Enter the number: 7852587
7852587 is a Palindrome

Leave a Comment

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