C Program to Check Palindrome Number
This program checks whether the entered integer is a palindrome number or not by reversing its digits and comparing it with the original number.
Concept Overview
A palindrome number remains the same when its digits are reversed. For example, 121, 1331, and 1221 are palindrome numbers. The program reverses the number using arithmetic operations and compares it with the original value.
Program
C
#include <stdio.h>
int main() {
int num, originalNum, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
reversed = reversed * 10 + remainder;
originalNum /= 10;
}
if (reversed == num)
printf("%d is a palindrome number.\n", num);
else
printf("%d is not a palindrome number.\n", num);
return 0;
}
Sample Output
Enter a number: 1221 1221 is a palindrome number. Enter a number: 1234 1234 is not a palindrome number.
Explanation
- The program stores the original number in a temporary variable.
- It reverses the number by repeatedly extracting digits using `%` and constructing the reverse using multiplication and addition.
- Finally, it compares the reversed number with the original number.
- If both are equal, it is a palindrome number.
Note: Note: This program works for positive integers. For negative numbers, the minus sign should be ignored during palindrome checking.
Codecrown