Home C++ Tutorial C++ Program to Check Whether a Number is Palindrome or Not

C++ Program to Check Whether a Number is Palindrome or Not

by Anubhav Tiwari
16 minutes read

In this article, you’ll learn about how to to Check Whether a Number is Palindrome or Not using C++

What is Palindrome Number?

An integer is a palindrome if the reverse of that number is equal to the original number.

#include <iostream>
using namespace std;
int main() {
    int num, reversed = 0, remainder, original;
    cout<<"Enter an integer: ";
    cin>>num;
    original = num;

    // reversed integer is stored in reversed variable
    while (num != 0) {
        remainder = num % 10;
        reversed = reversed * 10 + remainder;
        num /= 10;
    }

    // palindrome if orignal and reversed are equal
    if (original == reversed)
        cout<<"Entered number is a palindrome";
    else
        cout<<"Entered number is not a palindrome";

    return 0;
}

Output

Enter an integer: 1234321
Entered number is a palindrome.

Here, the user is asked to enter an integer. The number is stored in variable num.

We then assigned this number to another variable orignal. Then, the reverse of num is found and stored in reversed.

If original is equal to reversed, the number entered by the user is a palindrome.

related posts

Leave a Comment

Enable Notifications OK No thanks