13. Check for palindrome string/number.
#include <stdio.h>
int main()
{
char str[50], i=0;
int str_len;
int number, t, rev=0, rmndr, ch;
clrscr();
printf("Enter your choice\n");
printf("Enter 1to Check for palindrome string\n");
printf("Enter 2 to Check for palindrome number\n");
scanf("%d",&ch);
if(ch==1)
{
printf("Please enter a word to check Palindrome : ");
scanf("%s",str);
printf("\nEntered word: %s", str);
while(1)
{
str_len = strlen(str) - (i+1);
if (str[i] == str[str_len])
{
if (i == str_len || i+1 == str_len)
{
printf("\nEntered word
\"%s\" is a palindrome", str);
break;
}
i = i+1;
}
else
{
printf("\nEntered word \"%s\" is not a palindrome",
str);
break;
}
}
}
else
{
printf("Please enter a number to check Palindrome : ");
scanf("%d",&number);
printf("\nEntered number: %d", number);
t = number;
while (number > 0)
{
rmndr = number%10;
rev = rev*10 + rmndr;
number = number/10;
}
printf("\nReversed number: %d", rev);
if(t == rev)
{
printf("\nEntered number %d is a palindrome", t);
}
else
{
printf("\nEntered number %d is not a palindrome", t);
}
getch();
return 0;
}
getch();
No comments:
Post a Comment