In this program, You will learn how to check number is palindrome or not, using a while loop, or do a while loop.
a number is a palindrome if the reversed number is equal to the original number. for example 121, 101, 12321 are palindrome numbers.
#include<stdio.h>
int main()
{
int r,n,rev=0,copy;
printf(" \n enter the number:");
scanf("%d",&n);
copy=n;
while(n>0)
{
/*It will find the reverse of the input entered by the user.*/
rev=rev*10+n%10;
n=n/10;
}
// Compare the reverse of input with the temporary variable
if(copy==rev)
printf("\n %d is palindrom a number",copy);
else
printf("\n %d is not a palindrome number",copy);
return 0;
}
enter the number:121 121 is a palindrom number enter the number:123 123 is not a palindrome number
#include<stdio.h>
int main()
{
int r,n,rev=0,copy;
printf(" \n enter the number:");
scanf("%d",&n);
copy=n;
do{
/*it will revese number */
rev=rev*10+n%10;
n=n/10;
}
while(n>0);
if(copy==rev)
printf("\n %d is a palindrom number",copy);
else
printf("\n %d is not a palindrome number",copy);
return 0;
}
enter the number:121 121 is a palindrom number enter the number:123 123 is not a palindrome number
copy
to check it when we need to compare our original number with reverse numbern%10
and rev*10
will maintain their nth positioncopy
variable