C Code 20 _ C Program to find whether the given string or number is palindrome or not
Program:
#include<stdio.h>
int main()
{
int
ch,n,rev=0,r,i,j=0,l=0,s=0,t;
char
c[100],b[100];
printf("\n* *
* * Palindrome or not * * * *\n\n____ Menu ____\n\n1 - check whether the given
number is palindrome or not\n\n2 - check whether the given string is palindrome
or not");
printf("\n\nEnter your operation to perform from above menu (1 or
2) : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n* * * check whether the given number is palindrome or
not * * *");
printf("\n\nEnter a number : ");
scanf("%d",&n);
t=n;
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
printf("\n\nThe given number : %d",t);
printf("\n\nReverse of given number : %d",rev);
if(rev==t)
{
printf("\n\n~ ~ ~ The given number is a palindrome ~ ~ ~");
}
else
{
printf("\n\n~ ~ ~ The given number is Not a palindrome ~ ~
~");
}
break;
case 2:
printf("\n\n* * * check whether the given string is palindrome or
not(with out using inbuild string functions) * * *");
printf("\n\nEnter a String : ");
scanf("%s",c);
i=0;
while(c[i]!='\0')
{
l=l+1;
i=i+1;
}
for(i=l-1;i>=0;i--)
{
b[j]=c[i];
j++;
}
b[j]='\0';
printf("\n\nThe given string : %s",c);
printf("\n\nReverse of given string : %s",b);
for(i=0;i<l;i++)
{
if(c[i]==b[i])
{
s=s+1;
}
}
if(l==s)
{
printf("\n\n~ ~ ~ The given string is a palindrome ~ ~ ~");
}
else
{
printf("\n\n~ ~ ~ The given string is Not a palindrome ~ ~
~");
}
break;
default:
printf("\n\nEnter a valid choice - 1 or 2");
}
getch();
return 0;
}
Output:
Thank you ...
0 comments: