A palindrome is a word, phrase, number, or other sequence of symbols or elements that reads the same forward or backward. For example if we write “madam”, this string is same for forward and backward reading. Also number 121 is a palindrome as forward and backward reading return same value.
Now let’s write code for integer numbers
The output for above program
Now let’s write code for integer numbers
#include<stdio.h>
void main()
{
int i=0,n,m;
printf("Enter your number you want to check ");
scanf("%d",&n);
m=n;
while(n!=0)
{ i=(i*10)+(n%10);
n=n/10;
}
if(i==m)
{ printf("Your given Number %d is Palindrome",m);
}
else
{ printf("Your given Number %d is not Palindrome",m);
}
}
In above program we insert a “while loop” to reverse the number. Then check the number is Palindrome number. Now let’s see the outputvoid main()
{
int i=0,n,m;
printf("Enter your number you want to check ");
scanf("%d",&n);
m=n;
while(n!=0)
{ i=(i*10)+(n%10);
n=n/10;
}
if(i==m)
{ printf("Your given Number %d is Palindrome",m);
}
else
{ printf("Your given Number %d is not Palindrome",m);
}
}
Enter your number you want to check 121
Your given Number 121 is Palindrome
Enter your number you want to check 123
Your given Number 123 is not Palindrome
Now let’s do same operation on string, the code isYour given Number 121 is Palindrome
Enter your number you want to check 123
Your given Number 123 is not Palindrome
#include<stdio.h>
#include<string.h>
void main()
{
char m[50], n[50];
printf("Enter the string you want to check\n");
gets(m);
strcpy(n,m);
strrev(n);
if( strcmp(m,n) == 0 )
printf("Your given string %s is a Palindrome\n",m);
else
printf("Your given string %s is not a Palindrome\n",m);
}
Here we do some string operation, where we first copy of string from “m” to “n” then we reverse the string of “n” then check if string “m” and “n” is same or not.#include<string.h>
void main()
{
char m[50], n[50];
printf("Enter the string you want to check\n");
gets(m);
strcpy(n,m);
strrev(n);
if( strcmp(m,n) == 0 )
printf("Your given string %s is a Palindrome\n",m);
else
printf("Your given string %s is not a Palindrome\n",m);
}
The output for above program
Enter the string you want to check
madam
Your given string madam is a Palindrome
Enter the string you want to check code
Your given string code is not a Palindrome
madam
Your given string madam is a Palindrome
Enter the string you want to check code
Your given string code is not a Palindrome


0 comments:
Post a Comment