Python Practice › Loop Programs › Program 25
Palindrome number with a loop
Write a Python program to check whether a number is a palindrome using a loop.
Input
12321, 12345
Output
12321 palindrome 12345 not a palindrome
Approach
- Reverse the number with a loop and compare it with the original.
Solution
for original in [12321, 12345]: n, rev = original, 0 while n > 0: rev = rev * 10 + n % 10 n //= 10 print(original, "palindrome" if rev == original else "not a palindrome")Output
12321 palindrome 12345 not a palindrome
Line by line
Line 2
Work on a copy (n) so the original value is still available for the comparison.
Learn the concept
New to this topic? Read the lesson, then come back.