Definition
Same both ways
Digits match left-to-right and right-to-left.
A palindrome number reads the same forwards and backwards — like 121 or 7. This tutorial covers reversing digits with % 10 and // 10, listing palindromes in a range, a string alternative, a live checker, worked Python examples, edge cases, and complexity.
Same both ways
Digits match left-to-right and right-to-left.
% 10 / // 10
Build the digit reverse, then compare.
Before the loop
Keep a copy before you destroy n.
100..200
Reuse the helper for each candidate.
Try 121 / 123
Check any nonnegative integer live.
d = digits
One pass over the digits of n.
A palindrome number looks the same when its digits are reversed. The classic interview approach builds that reverse with a loop, then compares it to the original.
Single-digit values always pass. Numbers ending in 0 (except 0 itself) fail because leading zeros disappear in the reversed integer. This page focuses on nonnegative integers for beginner clarity.
It is a classic while-loop drill that combines remainder, integer division, and careful state saving.
rev = rev*10 + n%10.
original == reversed.
120 reverses to 21.
Always a palindrome.
In short: save the original, reverse digits in a loop, return whether they match.
Given a nonnegative integer, decide whether its decimal digits form a palindrome, and optionally list all palindromes in a range.
# 121 -> reverse 121 -> palindrome
# 123 -> reverse 321 -> not
# 120 -> reverse 21 -> not | Item | Type | Description |
|---|---|---|
number / n | int | Nonnegative integer to test. |
| Return | bool | True when original equals digit reverse. |
| Range print | text | Palindrome values from start through end. |
function is_palindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversed | Method | Idea | Notes |
|---|---|---|
| Arithmetic reverse | Loop with % / // | Interview default |
| String slice | str(n) == str(n)[::-1] | Short; mention after arithmetic |
| Two pointers on digits | Compare ends | Useful for digit arrays |
| Goal | Pattern |
|---|---|
| Last digit | digit = n % 10 |
| Append to reverse | rev = rev * 10 + digit |
| Drop last digit | n //= 10 |
| Compare | return original == rev |
| String check | str(n) == str(n)[::-1] |
| Inclusive range | range(100, 201) |
Same yes/no answer — different interview signals.
rev*10 + n%10This page — classic interview style
s == s[::-1]Short Python; show loops first
left / rightNatural for digit lists
save originalCopy n before the reverse loop
Reach for a digit-reverse palindrome check whenever symmetry of digits matters.
While loops, remainder, and comparison.
Practice % 10 and // 10 together.
List all palindromes in an interval.
Same idea as word palindromes.
Define negative behavior separately.
Key benefit: one reusable helper that teaches digit peeling, state saving, and symmetry checks.
Enter a nonnegative integer and check whether it is a palindrome.
Three complete Python programs — arithmetic check for 121, palindromes from 100 to 200, and a string-based alternative. Click View Output to reveal sample console results.
Arithmetic reversal and a single sample value.
Use arithmetic reversal and compare with the original.
def is_palindrome(number: int) -> bool:
original_number = number
reversed_number = 0
while number != 0:
remainder = number % 10
reversed_number = reversed_number * 10 + remainder
number //= 10
return original_number == reversed_number
number = 121
if is_palindrome(number):
print(f"{number} is a palindrome number.")
else:
print(f"{number} is not a palindrome number.") The loop peels digits from the right and builds reversed_number. For 121 the reverse is also 121, so the helper returns True.
Reuse the same helper inside an inclusive range loop.
Scan each candidate and print those that pass the check.
def is_palindrome(num: int) -> bool:
original_num = num
reversed_num = 0
while num != 0:
remainder = num % 10
reversed_num = reversed_num * 10 + remainder
num //= 10
return original_num == reversed_num
print("Palindrome numbers in the range 100 to 200:")
for i in range(100, 201):
if is_palindrome(i):
print(i, end=" ") Python range excludes the end, so use 201 to include 200. Three-digit palindromes in this band look like 1a1.
Compare the digit string with its reverse slice — short, but show arithmetic first in interviews.
def is_palindrome_str(number: int) -> bool:
s = str(number)
return s == s[::-1]
for n in (121, 123, 7, 120):
label = "palindrome" if is_palindrome_str(n) else "not a palindrome"
print(f"{n} is {label}.") s[::-1] reverses the characters of the digit string. Leading zeros never appear in str(n), so 120 still fails — matching the arithmetic rule.
Copy n before the loop mutates it.
digit = n % 10, then n //= 10.
rev = rev * 10 + digit each step.
Palindrome if original == rev.
Trace the reverse loop for n = 121.
| Step | n | digit | rev |
|---|---|---|---|
| Start | 121 | — | 0 |
| 1 | 12 | 1 | 1 |
| 2 | 1 | 2 | 12 |
| 3 | 0 | 1 | 121 |
original 121 equals rev 121 — palindrome.
Where palindrome-number checks show up beyond the interview prompt.
While loops and digit math.
Example: write is_palindrome.
List pals in an interval.
Example: 100..200.
Practice % 10 and // 10.
Example: reverse any n.
Trailing zeros and single digits.
Example: 120 vs 7.
Same idea as word palindromes.
Example: radar / level.
Continue the interview track.
Example: related CTA.
Pro Tip: say “save original, reverse with % and //, compare” before typing code.
Why the arithmetic reverse approach works well in interviews.
Dry-run 121 on paper and watch rev grow.
Shows comfort with integer arithmetic.
Same function powers single checks and ranges.
No overflow worries with Python ints.
Pro Tip: lead with arithmetic reverse; offer the string slice as a concise alternative.
Small habits that keep palindrome checks interview-ready.
Copy n before the reverse loop.
Call out 120 → 21 as a classic trap.
State whether negatives are allowed.
Use end + 1 with Python range.
Show arithmetic first, then s[::-1].
Pro Tip: dry-run 121 and 120 aloud — if both match the table, your reverse logic is solid.
Mistakes that commonly break palindrome-number programs.
Comparing after n is already zeroed out.
→ Copy original before the loop.
Thinking 120 should match 021.
→ Integer reverse drops leading zeros.
Missing the last value with exclusive end.
→ Use range(start, end + 1).
Undefined behavior for -121.
→ Reject or define a signed policy.
Skipping the arithmetic method in interviews.
→ Lead with the reverse loop.
Handle these before claiming the check is complete.
120 reverses to 21, so it is not a palindrome.
Always a palindrome for nonnegative numbers.
Zero is a palindrome.
This page rejects negatives in live preview for clarity.
Works the same — e.g. 1221.
Still O(d); Python ints grow as needed.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
Quick Takeaway: save original, reverse with % and //, return original == rev.
| Operation | Time | Extra space |
|---|---|---|
is_palindrome(n) | O(d) | O(1) |
Range [a, b] | O((b-a+1) * d) | O(1) |
| String slice check | O(d) | O(d) for the string |
Here d is the number of decimal digits in the value being tested.
Checking a palindrome number means reversing its digits and comparing with the original. Save a copy first, peel digits with % 10 and // 10, and watch for trailing zeros.
Practice the three examples above, then continue to generating Pascal’s triangle.
original == reverse(digits) — that is the whole check.
Check digit symmetry the interview-friendly way.
% and // loop
Patternoriginal == rev
Rule120 ≠ 21
Edge0..9 always yes
BaseO(d) / O(1)
AnalysisThe word palindrome also describes words like “radar” or “level”. For integers, we compare digits only, so single-digit values like 7 always pass.
Learn how to generate Pascal’s triangle rows with nested loops in Python.
8 people found this page helpful