Subscribe Us

Palindrome Number In Python

Palindrome Number In Python


Python Program To Check Palindrome number or Not

A Palindromic number (Also Known As a Numeral Palindrome or a Numeric palindrome)
is a number (such as 12321) that remains the same when its digits are reversed

Example :
Given Number : 121
It is a Palindrome Number
Since:
121 remains the same, when its digits are reversed
Example :
Given Number : 123
It is Not a Palindrome Number
Since:
123 do Not remain the same, when its digits are reversed

Algorithm To check if a number is a palindrome or not in Python
you can use the following algorithm:
  1. Read the input number.
  2. Create a copy of the input number.
  3. Initialize a variable to store the reverse of the number, initially set to 0.
  4. While the input of the number is not equal to 0, perform the following steps:
    • Get the last digit of the number by using the modulo operator (%).
    • Multiply the reverse variable by 10 and add the last digit to it.
    • Divide the input number by 10 to remove the last digit.
  5. Compare the copy number with the reverse variable.
  6. If they are equal, the number is a palindrome; otherwise, it is not.
  7. Print the result.

In this algorithm, the input number is processed to reverse its digits by repeatedly extracting
the last digit of the number, multiplying the reverse by 10, and adding the last digit to it.
Then, it checks if the reverse is equal to the input number to determine if it is a Palindrome Number.

Please note that this implementation assumes that the input number is a non-negative integer.
You may need to add additional checks for negative numbers or other cases depending on your
requirements.

Here is an example implementation in Python:

Source Code

#Python Program to Check Palindrome Number Or Not
num = int(input("Enter a Number to Check: "))
Rev=0
Ncopy = num
while num!=0:
    Rd  = num % 10
    Rev = Rev * 10 + Rd
    num = num //10

if Rev==Ncopy:
    print("It is a Palindorme Number ")
else:
    print("It is Not a Palindrome Number . ")

Output

Sample Input :
Enter a Number To Check : 121
Sample Output:
It is a Palindrome Number

Sample Input :
Enter a Number To Check : 124
Sample Output:
It is Not a Palindrome Number

Sample Input :
Enter a Number To Check : 13231
Sample Output:
It is a Palindrome Number