Python Practice › Loop Programs › Program 25

Palindrome number with a loop

EasyLoop Programs · 25 of 90

Write a Python program to check whether a number is a palindrome using a loop.

Advertisement
Input
12321, 12345
Output
12321 palindrome
12345 not a palindrome

Approach

  1. Reverse the number with a loop and compare it with the original.

Solution

Python
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.

More loop programs

← All 90 Loop Programs

Leave a Comment