Google News
logo
C++ Program to Check Whether a Number is Palindrome or Not
In the following example of C++ program that checks whether a given number is a palindrome or not :
Program :
#include <iostream>

using namespace std;

int main()
{
    int n, num, digit, rev = 0;
    cout << "Enter a positive integer: ";
    cin >> n;

    num = n;

    do {
        digit = num % 10;
        rev = (rev * 10) + digit;
        num = num / 10;
    } while (num != 0);

    if (n == rev) {
        cout << n << " is a palindrome number." << endl;
    } else {
        cout << n << " is not a palindrome number." << endl;
    }

    return 0;
}
Output :
Enter a positive integer: 12321
12321 is a palindrome number.
In this program, we first prompt the user to enter a positive integer using cout. We then read the integer input using cin. We store a copy of the integer in a variable called num.

We then use a do-while loop to reverse the digits of the integer and store the result in a variable called rev. Inside the loop, we first find the last digit of the integer using the modulo operator %, and then add it to the reversed integer by multiplying the reversed integer by 10 and adding the last digit. We then remove the last digit from the integer by dividing it by 10.

After the loop, we check if the original integer n is equal to the reversed integer rev. If they are equal, we print that n is a palindrome number. Otherwise, we print that n is not a palindrome number.