Press "Enter" to skip to content

C++: Input a Number and Check Whether it is Palindrome or Not

A palindrome is a string when the reverse of the string is exact equal to the string.

For example: For word or string ‘malayalam’; it’s reverse is also ‘malayalam’. So ‘malayalam’ is a palindrome.

For ‘cat’, the reverse is ‘tac’, which are not equal. So ‘cat’ is not a palindrome.

Here we check palindrome number using C++ program.

For number 12321, it’s reverse is also same. So 12321 is a palindrome.

#include <iostream>
using namespace std;

int main()
{
    int num,reverse_num = 0,num_temp;
    cout << "Enter a number :";
    cin>>num;
    num_temp = num;
    while(num_temp%10 != 0)
    {
        reverse_num = (reverse_num * 10) + (num_temp % 10);
        num_temp = num_temp/10;
    }
    if(num == reverse_num )
    {
      cout<<"The given number is palindrome";
    }
    else
    {
      cout<<"The given number is NOT palindrome";
    }
    return 0;
}

Sample output for palindrome

Sample output for non palindrome

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *