A palindrome is a number that remains the same when its digits are reversed. In this article, we will explore various C Program to Check Whether a Number is Palindrome or Not. We will cover multiple approaches with examples, explanations, and outputs.
Prerequisites
Before diving into the examples, you should be familiar with:
- Basic C syntax and structure.
- Loops and conditional statements.
- Functions in C.
Understanding Palindrome Numbers
A palindrome number is a number that is the same when read forward and backward. For example, 121 is a palindrome because it reads the same backward as forward.
Example 1: Using Iteration and Reversal
1.1 Explanation
The simplest way to check if a number is a palindrome is to reverse the digits of the number and compare it to the original number.
1.2 Program: Iteration and Reversal
#include <stdio.h>
int isPalindrome(int num) {
int reversed = 0, original = num, remainder;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
return original == reversed;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPalindrome(number))
printf("%d is a palindrome.\n", number);
else
printf("%d is not a palindrome.\n", number);
return 0;
}
1.3 Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.
Example 2: Using String Conversion
2.1 Explanation
Another way to check if a number is a palindrome is by converting the number to a string and then checking if the string reads the same backward.
2.2 Program: String Conversion
#include <stdio.h>
#include <string.h>
int isPalindrome(int num) {
char str[20], rev[20];
sprintf(str, "%d", num);
strcpy(rev, str);
strrev(rev);
return strcmp(str, rev) == 0;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPalindrome(number))
printf("%d is a palindrome.\n", number);
else
printf("%d is not a palindrome.\n", number);
return 0;
}
2.3 Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.
Example 3: Using Recursion
3.1 Explanation
We can also use recursion to check if a number is a palindrome by comparing the first and last digits of the number.
3.2 Program: Recursion
#include <stdio.h>
#include <math.h>
int reverse(int num, int temp) {
if (num == 0)
return temp;
temp = (temp * 10) + (num % 10);
return reverse(num / 10, temp);
}
int isPalindrome(int num) {
if (num == reverse(num, 0))
return 1;
return 0;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPalindrome(number))
printf("%d is a palindrome.\n", number);
else
printf("%d is not a palindrome.\n", number);
return 0;
}
3.3 Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.
Example 4: Using Array for Digits
4.1 Explanation
This method involves storing the digits of the number in an array and then checking if the array reads the same backward.
4.2 Program: Array for Digits
#include <stdio.h>
int isPalindrome(int num) {
int digits[10], temp = num, count = 0;
while (temp > 0) {
digits[count++] = temp % 10;
temp /= 10;
}
for (int i = 0; i < count / 2; i++) {
if (digits[i] != digits[count - i - 1])
return 0;
}
return 1;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPalindrome(number))
printf("%d is a palindrome.\n", number);
else
printf("%d is not a palindrome.\n", number);
return 0;
}
4.3 Output
Enter a number: 121
121 is a palindrome.
Enter a number: 123
123 is not a palindrome.
Conclusion
This article explored various C Program to Check Whether a Number is Palindrome or Not. We discussed simple iteration and reversal, string conversion, recursion, and using arrays to store digits. Each method has its unique advantages and understanding these different approaches allows you to choose the most appropriate solution for your specific needs.
By mastering these techniques, you can apply similar logic to other programming challenges and enhance your problem-solving skills in C.