Contact Learn C
Copy

Program 10:To find whether given number is palindrome or not

Program 10:

#include<stdio.h>
main() 
{
 int dummy,n,rev=0,x;
 printf("Enter a number\n");
 scanf("%d",&n);
 dummy=n;
 while(n>0)
 {
   x=n%10;
   rev=rev*10+x;
   n=n/10;
 }
 if(dummy==rev)
 printf("The given number %d is a palindrome\n",rev);
 else
 printf("The given number %d is not a palindrome\n",rev);

}
Explanation:
  1. This is same as reversing a number which is Program 9 . To print reverse of a number
  2. Just we used dummy variable to check whether the reversed number is same as the original one(i.e. 'n').
  3. for example if n=321=dummy then the reverse rev=123.
  4. As dummy is not equal to rev then the output is not a palindrome.(Here we can't use 'n' as it become 0 at the end of the loop which is why we are using a variable 'dummy' for temporary storage of the value).
Output: 

find whether given number is palindrome or not

No comments:

Post a Comment

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide