
saxenadivya859
Saturday, 2024-08-03
To check if a number is a palindrome in C, you need to determine whether the number reads the same forwards and backwards. Here’s a step-by-step approach to achieve this:
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0, remainder;
// Input the number from user
printf("Enter an integer: ");
scanf("%d", &num);
// Store the original number to compare later
originalNum = num;
// Reverse the number
while (num != 0) {
remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Append digit to reversed number
num /= 10; // Remove the last digit from num
}
// Check if the reversed number is equal to the original number
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
}
return 0;
}