Check whether an integer is a palindrome.
SOLUTION :
#include
#include
using namespace std;
int main()
{
int num;
cout<<"Enter a number to check if its a palindrome : ";
cin>>num;
int div=10;
int x;
int q;
int r;
if(num <0)
{
cout<<"\n\nNot a Palindrome\n\n";
exit(0);
}
while(num/div >=10)
{
div*=10;
}
while(num != 0)
{
q=num/div;
r=num%10;
if( q != r )
{
cout<<"\n\nNot a palindrome\n\n";
exit(0);
}
else
{
num=(num%div)/10;
div /= 100;
}
}
cout<<"\n\nA Palindrome\n\n";
}
No comments:
Post a Comment