This program I provided checks whether a given number is a palindrome or not. A palindrome number is a number that remains the same when its digits are reversed. For example, 121 is a palindrome because it remains the same when its digits are reversed.
The program first prompts the user to enter a number, then stores it in the originalNum
variable. It then reverses the number using a while loop and stores the reversed number in the reversedNum
variable.
If the reversed number is equal to the original number, the program outputs that the number is a palindrome, otherwise, it outputs that the number is not a palindrome.
- the program first declares several integer variables – num, reversedNum, remainder, and originalNum.
- It prompts the user to enter an integer using the
printf
andscanf
functions. - The program then stores the entered integer in the
originalNum
variable. - The program then reverses the entered integer using a
while
loop. This is done by continuously taking the remainder of the entered integer when divided by 10 and adding it to thereversedNum
variable, which is initially set to 0. Thenum
variable is then divided by 10 in each iteration to remove the last digit. - If the reversed number is equal to the original number, the program outputs that the number is a palindrome. Otherwise, it outputs that the number is not a palindrome.
#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder, originalNum;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
// reverse the integer
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
printf("%d is a palindrome.", originalNum);
else
printf("%d is not a palindrome.", originalNum);
return 0;
}


+ There are no comments
Add yours