C Program to Check if a Number is a Palindrome
Identifying palindromes, numbers that read the same backward and forward, has intrigued mathematicians and programmers for centuries. This guide explores writing a C program to efficiently check if a given number is a palindrome.
Understanding the Logic
There are two main approaches to check for palindromes in C:
1. String Conversion and Comparison:
- Convert the integer to a string using
sprintf
or other conversion functions. - Reverse the string using a loop or library functions like
strrev
. - Compare the original and reversed strings using
strcmp
. If they are equal, the number is a palindrome.
2. Digit Manipulation:
- Extract digits from the number one by one using loops and modulo operations.
- Build a reversed number by multiplying each extracted digit by appropriate powers of 10 and adding them together.
- Compare the original and reversed numbers. If they are equal, the number is a palindrome.
Implementing the String Conversion Approach
Here’s an example code snippet using string conversion:
C
#include <stdio.h>
#include <string.h>
int isPalindromeString(char numStr[]) {
char reversedStr[strlen(numStr) + 1];
// Copy the string in reverse
for (int i = 0, j = strlen(numStr) - 1; i < strlen(numStr); i++, j--) {
reversedStr[i] = numStr[j];
}
reversedStr[strlen(numStr)] = '\0';
// Compare the original and reversed strings
return strcmp(numStr, reversedStr) == 0;
}
int main() {
long int num;
printf("Enter a number: ");
scanf("%ld", &num);
char numStr[20];
sprintf(numStr, "%ld", num); // Convert number to string
if (isPalindromeString(numStr)) {
printf("%ld is a palindrome.\n", num);
} else {
printf("%ld is not a palindrome.\n", num);
}
return 0;
}
This code first converts the number to a string, then reverses the string, and finally compares the original and reversed strings. If they match, the number is a palindrome.
Implementing the Digit Manipulation Approach
Here’s an example using digit manipulation:
C
int isPalindrome(int num) {
int originalNum = num, reversedNum = 0;
while (num > 0) {
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return originalNum == reversedNum;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPalindrome(num)) {
printf("%d is a palindrome.\n", num);
} else {
printf("%d is not a palindrome.\n", num);
}
return 0;
}
This code extracts digits from the number one by one, builds a reversed number by adding them in reverse order with appropriate powers of 10, and finally compares the original and reversed numbers. If they match, the number is a palindrome.
Choosing the Right Approach
Both approaches have their advantages and disadvantages:
- String conversion: Easier to understand and implement for beginners, but may involve unnecessary string manipulation and be less efficient for large numbers.
- Digit manipulation: More efficient and avoids string operations, but requires understanding modulo operations and digit extraction techniques.
Ultimately, the choice depends on your project requirements, performance considerations, and personal preference.
Extending the Program
You can enrich your program by:
- Handling negative numbers (check for sign and convert to absolute value before checking for palindrome).
- Supporting different data types like characters or strings.
- Implementing multiple palindrome checking methods and comparing their performance.
By understanding the different approaches and exploring extensions, you can write versatile and efficient C programs to identify palindromes in diverse scenarios.
I hope this comprehensive guide provides a valuable resource for writing C programs to check if a number is a palindrome.