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 C 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
7 -> reverse 7 -> palindrome
120 -> reverse 21 -> not */ | Item | Type | Description |
|---|---|---|
number / n | int | Nonnegative integer to test. |
| Return | int | 1 when original equals digit reverse; else 0. |
| Range print | text | Palindrome values from start through end. |
function isPalindrome(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 % and / | Interview default |
| Char buffer | sprintf + two-index compare | 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 | sprintf + two-index compare |
| Inclusive range | for (i = 100; i <= 200; ++i) |
Same yes/no answer — different interview signals.
rev*10 + n%10This page — classic interview style
sprintf + endsShort C; 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 C 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.
#include <stdio.h>
int isPalindrome(int number) {
int originalNumber = number;
int reversedNumber = 0;
int remainder;
while (number != 0) {
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
return originalNumber == reversedNumber;
}
int main(void) {
int number = 121;
if (isPalindrome(number)) {
printf("%d is a palindrome number.\n", number);
} else {
printf("%d is not a palindrome number.\n", number);
}
return 0;
} The loop peels digits from the right and builds reversedNumber. For 121 the reverse is also 121, so the helper returns 1 (true).
Reuse the same helper inside an inclusive range loop.
Scan each candidate and print those that pass the check.
#include <stdio.h>
int isPalindrome(int num) {
int originalNum = num;
int reversedNum = 0;
int remainder;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
return originalNum == reversedNum;
}
int main(void) {
int i;
printf("Palindrome numbers in the range 100 to 200:\n");
for (i = 100; i <= 200; ++i) {
if (isPalindrome(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} The for loop uses i <= 200 so 200 is included. Three-digit palindromes in this band look like 1a1.
Write digits with sprintf, then compare ends with two indices — short, but show arithmetic first in interviews.
#include <stdio.h>
#include <string.h>
int isPalindromeStr(int number) {
char s[32];
int i, j;
sprintf(s, "%d", number);
i = 0;
j = (int)strlen(s) - 1;
while (i < j) {
if (s[i] != s[j]) {
return 0;
}
i++;
j--;
}
return 1;
}
int main(void) {
int tests[] = {121, 123, 7, 120};
int i, n;
for (i = 0; i < 4; ++i) {
n = tests[i];
if (isPalindromeStr(n)) {
printf("%d is palindrome.\n", n);
} else {
printf("%d is not a palindrome.\n", n);
}
}
return 0;
} Two indices walk from both ends of the sprintf buffer until they meet. Leading zeros never appear in that buffer, 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 isPalindrome.
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 C ints.
Pro Tip: lead with arithmetic reverse; offer the sprintf buffer check 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 C range.
Show arithmetic first, then sprintf + ends.
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.
Stopping the loop before end inclusive.
→ Use i <= end (inclusive) in the for loop.
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); C 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 |
|---|---|---|
isPalindrome(n) | O(d) | O(1) |
Range [a, b] | O((b-a+1) * d) | O(1) |
| sprintf buffer 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.
i <= endCheck 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 C.
8 people found this page helpful