Palindrome Number Program in C with Output
Check whether an integer reads the same forwards and backwards in C by reversing its digits and comparing with the original.
- Time
O(d), - Space
O(1) - Hints3
- Read3 min
A palindrome number reads the same forwards and backwards, like 121 or 1001. In C, save the number in original, then build the reverse with a while loop: reversed = reversed * 10 + n % 10; n = n / 10;. When n becomes 0, compare original with reversed. If they are equal, it is a palindrome. 121 is a palindrome, 123 is not.
Problem statement
Write a palindrome number program in C. Read an integer and print whether it is a palindrome.
Use the reverse-and-compare method: reverse the digits with arithmetic, then compare the reverse with the original number. Show the output for 121 and 123.
A single integer n.
"<n> is a palindrome number" or "<n> is not a palindrome number".
Examples
n = 121121 is a palindrome numberThe reverse of 121 is 121, which equals the original.
n = 123123 is not a palindrome numberThe reverse of 123 is 321, which is different.
n = 10011001 is a palindrome numberThe reverse of 1001 is 1001.
Constraints
- n fits in a 32-bit int
- Negative numbers are treated as not palindromes
Think before you code
Reveal the questions to ask yourself first
- How do you get the last digit of
n, and how do you remove it? - The loop changes
nto 0. What do you compare with at the end? - Can the reverse of a valid
intbe too large for anint?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Palindrome logic in C: reverse and compare
- Read
nand copy it:original = n. - Set
reversed = 0. - While
n > 0:digit = n % 10reversed = reversed * 10 + digitn = n / 10
- If
original == reversed, the number is a palindrome.
In C, dividing two int values with / drops the decimal part, so 121 / 10 is 12.
That is exactly what removes the last digit.
| n | Reverse | Palindrome? |
|---|---|---|
| 121 | 121 | Yes |
| 123 | 321 | No |
| 1001 | 1001 | Yes |
| 10 | 1 | No |
Dry run
Walk through the example step by step
n = 121, original = 121, reversed = 0:
n | digit = n % 10 | reversed = reversed * 10 + digit | n / 10
----+----------------+----------------------------------+-------
121 | 1 | 1 | 12
12 | 2 | 12 | 1
1 | 1 | 121 | 0
original (121) == reversed (121), so 121 is a palindrome.
For n = 123 the same steps give reversed = 321. 123 != 321, so it is not.
Solution
Reveal the full C solution
#include <stdio.h>
int main(void) {
int n, original, reversed = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while (n > 0) {
digit = n % 10;
reversed = reversed * 10 + digit;
n = n / 10;
}
if (original == reversed) {
printf("%d is a palindrome number\n", original);
} else {
printf("%d is not a palindrome number\n", original);
}
return 0;
}Output (two separate runs):
Enter a number: 121
121 is a palindrome number
Enter a number: 123
123 is not a palindrome number
The program prints original, not n, because n is 0 after the loop.
Check palindrome in C with a function
A function makes the check reusable. Here the reverse is stored in long long, so it
cannot overflow even for the largest int values.
#include <stdio.h>
int isPalindrome(int n) {
if (n < 0) {
return 0;
}
long long reversed = 0;
int temp = n;
while (temp > 0) {
reversed = reversed * 10 + temp % 10;
temp /= 10;
}
return reversed == n;
}
int main(void) {
int tests[] = {121, 123, 0, 1001, 10, -121, 2147447412};
int count = sizeof(tests) / sizeof(tests[0]);
for (int i = 0; i < count; i++) {
printf("%d -> %s\n", tests[i], isPalindrome(tests[i]) ? "palindrome" : "not palindrome");
}
printf("Palindromes from 100 to 200: ");
for (int n = 100; n <= 200; n++) {
if (isPalindrome(n)) {
printf("%d ", n);
}
}
printf("\n");
return 0;
}
121 -> palindrome
123 -> not palindrome
0 -> palindrome
1001 -> palindrome
10 -> not palindrome
-121 -> not palindrome
2147447412 -> palindrome
Palindromes from 100 to 200: 101 111 121 131 141 151 161 171 181 191
Why the reverse can overflow
The largest 32-bit int is 2147483647. A number like 1000000009 fits, but its reverse
9000000001 does not. With an int reverse, signed overflow is undefined behaviour in
C, so the result cannot be trusted. Storing the reverse in long long, as in the
function above, avoids this.
Palindrome check with a string
If the number is read as text, you can compare characters from both ends without any arithmetic.
#include <stdio.h>
#include <string.h>
int main(void) {
char s[20];
printf("Enter a number: ");
scanf("%19s", s);
int len = strlen(s);
int isPal = 1;
for (int i = 0; i < len / 2; i++) {
if (s[i] != s[len - 1 - i]) {
isPal = 0;
break;
}
}
printf("%s\n", isPal ? "Palindrome" : "Not a palindrome");
return 0;
}
Enter a number: 12321
Palindrome
Most lab questions ask for the arithmetic method, so learn that one first.
The Java version is check whether a number is a palindrome, and the Python version for numbers and strings is the palindrome program in Python. More lab programs are in C programs for beginners.
O(d), where d is the number of digitsO(1)Review before the interview
Common mistakes
- Comparing reversed with n after the loop. n is 0 by then; compare with the saved original.
- Not initialising reversed to 0. A local int starts with garbage in C, so the reverse comes out wrong.
- Using an int for the reverse of large inputs such as 1000000009. The reverse overflows; use long long.
- Writing reversed = digit * 10 + reversed. That adds digits in the wrong place and gives the original order.
Edge cases to test
- 0 is a palindrome: the loop does not run, reversed stays 0, and 0 == 0.
- Numbers ending in 0, like 10 or 120, are never palindromes (except 0).
- Negative numbers such as -121 are treated as not palindromes because of the minus sign.
- Single-digit numbers 1 to 9 are always palindromes.
Interview follow-ups
- How would you check a palindrome by reversing only half of the digits?
- How would you check whether a string, not a number, is a palindrome in C?
- How would you print all palindrome numbers between 1 and 1000?
Keep practising
Check Whether a Number Is a Palindrome
Hints · Dry run · SolutionSolve Similar problemPalindrome Program in Python (Number and String)
Hints · Dry run · SolutionSolve Similar problemReverse a Number Without Using Strings
Hints · Dry run · SolutionSolve Similar problemCheck Whether a String Is a Palindrome
Hints · Dry run · SolutionSolve Related learningOperators In C
TutorialLearn Related learningLoops In Programming
TutorialLearn Related learningC Programming Interview Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartBuilding your programming basics?
CodeBegun’s programs build problem-solving with mentor review and mock interviews, online or in Madhapur, Hyderabad.
