Check Palindrome Number in C
What you’ll learn
- What a palindrome number is (same digits forward and backward).
- How to reverse digits with
% 10and/ 10, then compare to the original. - A second program that prints every palindrome between
100and200, plus a live preview.
Overview
Think of 121: front to back is “1-2-1,” and back to front is the same. The program peels off the last digit repeatedly, builds the reversed number, and checks equality.
Single check
Try 121 or 12321.
Range scan
Every palindrome from 100 to 200.
Live preview
Nonnegative integers only (matches the C mindset here).
Prerequisites
while loops, integer division, and % for the last digit.
#include <stdio.h>,int main(void),printf.- Comfort reading multi-digit integers digit-by-digit.
The idea
Take 121. Chop the last digit (1), remember it. Chop again (2), shift what you remembered left and add. Chop again (1). You rebuilt 121—if that equals what you started with, it is a palindrome.
Same trick works for 9009, 5, or 0.
Live preview
Enter a nonnegative integer (same convention as the C samples). Reverses digits the same way as Example 1.
Algorithm
Goal: decide whether the decimal digits of n read the same forward and backward.
Remember original
You will change a working copy of n; keep the first value safe.
Reverse digits
Repeatedly: digit = n % 10, rev = rev * 10 + digit, n /= 10 until n == 0.
Compare
If rev == original, you have a palindrome.
📜 Pseudocode
function isPalindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversed Check one number
Classic interview structure: 121 reads 121 backward too.
#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;
} Explanation
The loop strips digits from number until it becomes 0. reversedNumber accumulates the flipped version; comparing with originalNumber finishes the test.
Palindromes from 100 to 200
Three-digit palindromes in this band look like aba: hundreds digit equals units digit. The loop prints each hit.
#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) {
printf("Palindrome numbers in the range 100 to 200:\n");
for (int i = 100; i <= 200; ++i) {
if (isPalindrome(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
} Explanation
Same helper as Example 1; the outer for walks every candidate in the inclusive range.
Notes
Overflow. For very large inputs, reversedNumber * 10 can overflow a 32-bit int; interview problems sometimes use long long or treat the number as a string.
Alternatives. Converting to a string and comparing indices from both ends avoids arithmetic overflow but needs extra memory.
❓ FAQ
🔄 Input / output examples
Swap number in Example 1 or change the loop bounds in Example 2.
| Value | Palindrome? |
|---|---|
0 | Yes |
7 | Yes |
121 | Yes |
123 | No (321) |
Edge cases
120 vs 21
Integer reversal drops trailing zeros on the right (they become leading zeros, which disappear). So 120 reverses to 21—not a palindrome unless you treat numbers with leading-zero strings.
n < 10
Always palindromes for nonnegative n.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isPalindrome(n) | O(d) digits | O(1) |
Range [a, b] | O((b-a+1) · d) worst case | O(1) |
Summary
- Core trick: reverse digits with
% 10and/ 10, compare to the saved original. - Ranges: reuse
isPalindromeinside aforloop. - Watch-outs: overflow on huge values; negatives need a separate plan.
The word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.
8 people found this page helpful
