Palindrome Number Check in Python
Check whether an integer is a palindrome using digit reversal.
BeginnerLoop ProgramsExample 8 of 25
palindrome-number-check.py
Run in browser1# Program to check palindrome number23num = int(input("Enter an integer: "))45temp = abs(num)6rev = 078while temp > 0:9 digit = temp % 1010 rev = rev * 10 + digit11 temp //= 101213if num >= 0 and rev == num:14 print(num, "is a palindrome")15else:16 print(num, "is not a palindrome")
Output
Enter an integer: 121 121 is a palindrome
What's going on
We reverse the digits and compare with the original number; for simplicity, we treat negative numbers as non-palindromes.