Check if a number is palindrome

Hello! Today I am going to show you how to determine whether a number is a palindrome or not using a basic algorithm.

First of all, what is a palindrome number?

A palindrome number is a number that reads the same backwards as forward.

Now the code :

#include <iostream>

using namespace std;

int main()
{
   int num, reverse_num=0, remainder,temp;
   cout<<"Enter an integer : ";
   cin>>num;

   /* Here we are generating a new number (reverse_num)
    * by reversing the digits of original input number
    */
   temp=num;
   while(temp!=0)
   {
      remainder=temp%10;
      reverse_num=reverse_num*10+remainder;
      temp/=10;
   }

   /* If the original input number (num) is equal to
    * to its reverse (reverse_num) then its palindrome
    * else it is not.
    */
   if(reverse_num==num)
      cout<<num<<" is a palindrome number"<<endl;
   else
      cout<<num<<" is not a palindrome number"<<endl;
   return 0;
}

And this is how you check whether a number is a palindrome or not, I hope this helped you!

Keep on coding ;)!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s