Signup/Sign In
LAST UPDATED: OCTOBER 12, 2020

C++ Palindrome Check for a given Number

Hello Everyone!

In this tutorial, we will learn how to check if the given Number is Palindrome or not, in the C++ programming language.

Condition for a Number to be Palindrome:

A number which is equal to its reverse.

Steps to check if the Number is Palindrome:

  1. Compute the reverse of the given number.

  2. If the number is equal to its reverse, it is Palindrome else it is not.

Code:

#include <iostream>
#include <math.h>

using namespace std;

//Returns true if the given number is a Palindrome number
bool isPalindrome(int n)
{
    int reverse = 0; //to store the reverse of the given number
    int remainder = 0;

    int n1 = n; //storing the original number for comparing later
    //logic to compute the reverse of a number
    while (n != 0)
    {
        remainder = n % 10;
        reverse = reverse * 10 + remainder;
        n /= 10;
    }

    if (reverse == n1)
        return true;
    else
        return false;
}

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to determine if the entered number is Palindrome or not ===== \n\n";

    //variable declaration
    int n;
    bool palindrome = false;

    //taking input from the command line (user)
    cout << " Enter a positive integer :  ";
    cin >> n;

    //Calling a method that returns true if the number is Palindrome
    palindrome = isPalindrome(n);

    if (palindrome)
    {
        cout << "\n\nThe entered number " << n << " is a Palindrome number.";
    }
    else
    {
        cout << "\n\nThe entered number " << n << " is not a Palindrome number.";
    }

    cout << "\n\n\n";

    return 0;
}

Output:

C++  palindrome program output 1

Let's try another input,

C++  palindrome program output 2

We hope that this post helped you develop better understanding how to check if the given number is Palindrome or not in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.