Checking for a Palindrome In C++


Write a C++ program to check if a number is a palindrome.

Click Here to View the Solution:
#include <iostream>
using namespace std;
int main()
    int x, y, number, reverse = 0;
    cout << "Enter a positive number:\n";
    cin >> number;
    x = number;
        y = number % 10;
        reverse = (reverse * 10) + y;
        number = number / 10;
    while (number != 0);
    cout << "Its reverse is: " << reverse << endl;
    if (x == reverse)
    cout << "The number is a palindrome";
    cout << "The number is not a palindrome";
    return 0;
Click Here to View the Output:
Enter a positive number:
Its reverse is: 23632
The number is a palindrome
Click Here to View the Explanation:
  • Some integers are declared in the beginning x, y, number, and reverse is assigned a value of 0.
  • The user is requested to enter a number and it is saved as x variable.
  • Then do... while loop is used to reverse the number by dividing the number by 10 and remainder is noted.
  • The number is reversed and saved simultaneously in another variable reverse.
  • Then if... else statement is used to check if the reversed number is equal to the number entered by the user.