Program to Check whether a String is Palindrome in C++
Following is a program to check whether a string is palindrome or not.
#include<iostream.h>
#include<string.h>
void main()
{
char string[50];
int len, i, x, flag=0;
cout<<"Enter a String(max 49 characters): ";
cin.getline(string,50);
len = strlen(string);
for(i=0, x=len-1;i<=len/2;i++, x--)
{
if(string[i]!=string[x])
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<"\n\nString is palindrome";
}
else
{
cout<<"\n\nString is not a palindrome";
}
}
Enter a String(max 49 characters) : Studytonight
String is palindrome
Program to Check whether a Number is Palindrome or not
Following is a program to check whether a number is palindrome or not.
#include<iostream.h>
#include<string.h>
void main()
{
int num, rev_num=0, c_num, ld;
cout<<"Enter a number : ";
cin>>num;
c_num=num;
while(num>0)
{
ld=num%10;
rev_num=(rev_num*10)+ld;
num /= 10;
}
if(rev_num == c_num)
{
cout<<endl<<c_num<<" is a palindrome";
}
else
{
cout<<endl<<c_num<<" is not a palindrome";
}
}
Enter a number : 121
121 is a palindrome