Checking if a number is a palindrome can be pretty useful. For example, 121 reads the same way forward and backward, so it’s a palindrome. How can we do this in a C program? Let’s dive in.
Prerequisites for the Palindrome Program in C
Before diving into the code, ensure you're familiar with these:
- Using
whileloops for iteration. - The modulo operator
%for obtaining remainders. - Basic concepts of reversing numbers.
- Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Check Whether a Number is Palindrome or Not
Below is the full code to check if a given number is a palindrome.
#include <stdio.h>
int main() {
int n, original, reverse = 0, rem;
// Input number from user
printf("Enter a number: ");
scanf("%d", &n);
original = n;
// Process each digit of the number
while (n != 0) {
rem = n % 10;
reverse = reverse * 10 + rem;
n /= 10;
}
// Check if original and reversed numbers are the same
if (original == reverse)
printf("%d is a palindrome.\n", original);
else
printf("%d is not a palindrome.\n", original);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Palindrome Program
Let's see what happens when we run the code with an example input.
Enter a number: 121
121 is a palindrome.
Algorithm to Check a Number for Palindrome in C
A palindrome reads the same forwards and backwards. Here's how this program figures it out:
- Read the integer
nfrom the user. - Initialize
original = nandreverse = 0. - While
nisn't zero, do the following: - Get the last digit
rem = n % 10. - Update the reverse number
reverse = reverse * 10 + rem. - Remove the last digit from
nby dividing it by 10. - Finally, compare
originalandreverse. - If they match, the number is a palindrome; otherwise, it's not.
flowchart TD
A([Start]) --> B[/Read n/]
B --> C[original = n]
C --> D[reverse = 0]
D --> E{"n != 0?"}
E -->|Yes| F["rem = n %% 10"]
F --> G["reverse = reverse * 10 + rem"]
G --> H[n = n / 10]
H --> E
E -->|No| I{"original == reverse?"}
I -->|Yes| J([Print "Palindrome"])
I -->|No| K([Print "Not a palindrome"])
J --> L([End])
K --> LHow the Palindrome Check Works
Understanding the line-by-line workings of this code will demystify how it checks for palindromes.
- The program begins by including the
<stdio.h>library, essential for input and output functions. - We declare
intvariables:n,original,reverse, andremto store various values. - We prompt the user to enter a number using
printf, and capture it withscanf. - Store the original number to
originalfor final comparison. - A
whileloop runs as long asnis not zero, processing one digit at a time. - Within the loop, the last digit is extracted using
rem = n % 10. - We update
reverseby appending the extracted digit, effectively reversing the number. - The number
nis then divided by 10 to remove the last processed digit, preparing for the next iteration. - Once all digits are processed, we compare the
originalnumber withreverse. - If they match,
printfconfirms the number is a palindrome; otherwise, it's not. - The program ends by returning
0, indicating successful execution.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is the key check to determine if a number is a palindrome?
How Fast is the Palindrome Check in C?
Let's examine the efficiency of this palindrome checking program.
Time Complexity: O(d). Here, d is the number of digits in the number being checked — 3 for 121. The loop while (n != 0) runs once per digit, making the process linear to the digit count. So, 5 digits mean 5 rounds through the loop.
Space Complexity: O(1). Though it processes an entire number, it uses a constant amount of additional space: n, original, reverse, and rem — four integers, no matter how many digits.
Common mistake:Not initializing reverse to zero can mess up your results.
Key takeaway:A palindrome check means reversing the digits, so it's all about comparing the original and the reverse.
FAQs About Checking a Number for Palindrome in C
Here are some common questions about this program, answered simply.
int type.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Compute Quotient and Remainder Easily
- C Program to Generate Multiplication Table with Example
- C Program to Count Number of Digits in an Integer
- C Program to Reverse a Number with Example
- C Program to Find Factorial of a Number
Conclusion
Palindrome checks are a classic exercise in reversing logic. The same idea pops up in many real-world scenarios, from computer science to genetics. Into strings? Give writing a program that checks palindrome words a shot next!
Keep practicing with more C puzzles and challenge yourself in our language quiz. Or check out more in the online compiler.