Write a C program to check whether a number is a palindrome
Description: Take a number as input. Print if it is a palindrome (it reads the same from both sides), like 121.
Hint:
- Reverse the number, and compare it with the original.
C program to check a palindrome number
#include <stdio.h>
int main(void) {
int n, original, reversed = 0;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
if (original == reversed) {
printf("%d is a palindrome\n", original);
} else {
printf("%d is not a palindrome\n", original);
}
return 0;
}
Output:
Output of the above program is
Enter a number: 121
121 is a palindrome
How it works
- We keep a copy in
original, because the loop changesn. - 121 reversed is 121, so both are equal.
Read Reverse a Number.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |