C Program to check whether a number is palindrome or not
Palindrome is a number which remains same after reversing its digits.
For example:
Consider a number 121.
Reverse of 121 is 121, so it is said to be palindrome.
On the other hand, number 1234 whose reverse is 4321 is not a palindrome.
CODE:
#include <stdio.h>
int main()
{
int num,rem,rev=0;
printf("Enter a number: ");
scanf("%d",&num);
int temp=num;
while(num>0){
rem=num%10;
num=num/10;
rev=(rev*10)+rem;
}
if(rev==temp){
printf("%d is a palindrome",temp);
}
else{
printf("%d is not a palindrome",temp);
}
return 0;
}
Note:
We are storing the value of num into another variable temp, because after the execution of while loop the value of num will be changed.
So we need the initial value of num so that we can compare the result after finding the reverse.
OUTPUT:
Comments
Post a Comment