A palindrome number is a number that reads the same forward and backward. For example, 121
, 1331
, and 12321
are palindromes. Checking whether a number is a palindrome is a classic programming exercise that helps beginners understand loops, conditional statements, and number manipulation. In this tutorial, we will write a complete C program to check if a number is a palindrome, explain every line in detail, and highlight common mistakes. You will also see both iterative and recursive methods to solve this problem.
Understanding palindromes is important because this concept appears in many programming problems, such as string manipulation, reversing numbers, and validating inputs. By the end of this post, you will know how to handle numbers step by step, check conditions, and implement logic in C.
Iterative Method to Check a Palindrome
The iterative approach reverses the number and compares it with the original number. If both are equal, the number is a palindrome.
#include <stdio.h>
int main() {
int num, originalNum, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
// check for negatives first
if (num < 0) {
printf("%d is not a palindrome (negative numbers not allowed).\n", num);
return 0;
}
originalNum = num; // Save the original number
while (num != 0) {
remainder = num % 10; // Extract the last digit
reversed = reversed * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit
}
if (originalNum == reversed)
printf("%d is a palindrome.\n", originalNum);
else
printf("%d is not a palindrome.\n", originalNum);
return 0;
}
In this program, originalNum
stores the original number to compare later with reversed
. The while
loop extracts each digit using the modulo operator %
and constructs the reversed number. Integer division /
removes the last digit. After the loop, the program checks if the reversed number equals the original number. If they are equal, it prints that the number is a palindrome; otherwise, it prints that it is not.
Recursive Method to Check a Palindrome
Recursion provides an elegant alternative to the iterative method. The idea is to reverse the number recursively and compare it with the original number.
#include <stdio.h>
int reverseNumber(int num, int reversed) {
if (num == 0)
return reversed;
return reverseNumber(num / 10, reversed * 10 + num % 10);
}
int main() {
int num, reversed;
printf("Enter an integer: ");
scanf("%d", &num);
// check for negatives first
if (num < 0) {
printf("%d is not a palindrome (negative numbers not allowed).\n", num);
return 0;
}
reversed = reverseNumber(num, 0);
if (num == reversed)
printf("%d is a palindrome (recursive).\n", num);
else
printf("%d is not a palindrome (recursive).\n", num);
return 0;
}
Here, the reverseNumber()
function works like in the previous tutorial. It takes num
and reversed
as arguments. The base case occurs when num
becomes zero. Each recursive call processes one digit from the end of num
and adds it to reversed
. Finally, the reversed number is compared with the original number to check if it is a palindrome.
How the Recursive Method Works
Suppose the input number is 121
. The recursive calls work as follows:
- Call 1:
reverseNumber(121, 0)
→ callsreverseNumber(12, 1)
- Call 2:
reverseNumber(12, 1)
→ callsreverseNumber(1, 12)
- Call 3:
reverseNumber(1, 12)
→ callsreverseNumber(0, 121)
- Base case reached → returns
121
The program then compares 121
(reversed) with the original 121
and confirms it is a palindrome.
Common Beginner Mistakes
When checking for palindromes, beginners often make these errors:
- Not storing the original number before modifying it, which prevents a correct comparison at the end.
- Forgetting that integer division truncates digits, which is required for building the reversed number properly.
- Missing the base case in recursion, leading to infinite calls and crashes.
- Using the wrong data type. Very large numbers can overflow an
int
, solong long
is a safer choice.
To avoid these problems, always save the original number, use integer division correctly, include a proper base case in recursive solutions, and pick a data type that can handle the expected input size.
FAQs
Q1: Can this program handle negative numbers?
Negative numbers are not considered palindromes because of the negative sign. If you enter -121
, the program will print that it is not a palindrome.
Q2: Which method is better, iterative or recursive?
The iterative method is simpler and safer for large numbers. Recursive method is elegant and easy to read but can cause stack overflow for very large numbers.
Q3: Can palindromes include decimals?
This program only works with integers. To handle decimals, you need to convert the number to a string and then check for a palindrome.
Conclusion
Checking if a number is a palindrome in C is a great exercise for beginners. It reinforces understanding of loops, recursion, and arithmetic operations. Both iterative and recursive methods are useful, and learning both approaches strengthens your programming skills. Keep practicing with different numbers to fully grasp the logic behind palindromes.
References & Additional Resources
- Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
- GeeksforGeeks: Check if a Number is Palindrome – Detailed examples and explanations.
- Programiz: C Programming Examples – Beginner-friendly C programs.
- TutorialsPoint: Recursion in C – Guide to recursion and loops.
- Stack Overflow: Reverse Integer in C – Handling large numbers safely.