"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Sunday, February 28, 2010

WAP to check a number is palindrome or not

eg.  
10201 is a palindrome
10210 is not a palindrome


#include<iostream.h>
#include<conio.h>

void main()
{
int num, rev=0;

cout<< "\n Enter a number :";
cin>> num;

/* truncate each digit from number and store the reverse of the number in rev */
int digit;
for(int i = num; i >0 ; i = i/10)
{
digit = i % 10;  // get the last digit
rev = rev* 10 + digit;
}

if( rev == num )
cout<< "\n The " << num  << " is a Palindrome. ";
else
cout << " \n The " << num << " is not a Palindrome. ";

getch();

}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.