Program to check if number is palindrome or not

Program to check if number is palindrome or not

A palindrome is number or letter which is same even if you read it from start to end or you read it from end to start.
e.g: level is palindrome.
789987 is palindrome.

Program:

#include<stdio.h>
void main()
{
int n,a,b,c=0;
printf("Enter number of which you want to test: ");
scanf("%d",&n);
a=n;
while(a!=0)
{
b=a%10;
c=(c*10)+b;
a=a/10;
}
if(n==c)
{
printf("Palindrome!!!");
}
else
{
printf("Not Palindrome!!!");
}
}

Output:








Explanation:

Suppose user enters 12321 as value.
So, in first iteration we will get,
a=12321
b=1
c=1
a=1232

In second iteration we will get,
a=1232
b=2
c=10+2=12
a=123

In third iteration we will get,
a=123
b=3
c=120+3=123
a=12

In fourth iteration we will get,
a=12
b=2
c=1230+2=1232
a=1

In fifth iteration we will get,
a=1
b=1
c=1232+1=12321
a=0   

Then while condition will not be followed as a=0.
and as n=c (in this case)
Output will be palindrome as shown in 2nd screenshot above.

No comments:

Post a Comment