C++ Program to check if a given number is Palindrome or not

In this blog post, you will learn how to write a C++ Program to check if a given number is Palindrome or not. But before writing the C++ code if a given number is Palindrome or not first let’s understand what a palindrome number is.

A number is called a palindrome number if the reverse of the given number is the same as the original string. For example,

Example 1:
Input: num = 11
Output: Palindrome Number
Explanation: reverse of 11 is 11.Since these are two same numbers 121 is a palindrome.


Example 2:
Input: num =  112 
Output: Palindrome Number
Explanation: reverse  of 112 is 211.
Since these are two different numbers 112 is not a palindrome.

 

Problem Statement: Given an integer number, check whether it is a palindrome number or not.

Constraints:

-231 <= x <= 231 – 1

Disclaimer: Try to solve the problem yourself first otherwise it would be not worth solving this problem.

 

Solution:

The solution to this problem is straightforward. You just need to reverse the original number and compare the reversed number with the original number. If both are the same, that means it is a palindrome number.

Now I suppose you are thinking about how to reverse a number.

Don’t worry I will explain it.

 

Suppose N is a given number. So declare a variable rev to store the reverse number. Initially initialize the rev with 0.  Now make a copy of N, suppose temp is a variable that has the copy of N.

Now let’s understand the procedure to reverse a number.

Step 1Isolate the last digit in the number:

The modulo operator (%) returns the remainder of a division. Using the help of the % operator at every execution we can extract the last digit of a given number.

For example, suppose 1234 is an integer number. If we perform the modulo division on it with 10, then we get the 4 as a remainder. That means we are successfully able to extract the last digit of a given integer number.

So,

rm = tmp%10;

 

Step 2 — Append the last digit to reverse:

Now append the extracted last digit, rm to rev using a formula ((10*rev) + rm). It would look like the below expression,

rev= (rev* 10) + rm;

 

Step 3-Remove the last digit from the number:

Now you have already used the last digit of the given number tmp, so remove this digit. To remove the last digit from a given integer number, you must divide it by 10.

tmp = tmp/10;

The above expression removes the last digit from the tmp; it happens because we are performing an integer division that rounds results down to the nearest integer, for example, 1245 / 10 => 124.

Now you need to do the same steps till tmp becomes 0 which means there are no new digits left to be reversed in tmp.

Now after successfully reversing the number it is time to check whether your given number is palindrome or not.

If rev equals the (original number) N then it’s a palindrome number” otherwise it is not a palindrome number.

C++ Code to check whether the given number is a palindrome :

/*
C++ Code to check whether
the given number is a palindrome :
*/
#include <iostream>
using namespace std;

bool checkPalindrome(int N)
{
    bool isPalindrome = false;
    if(N >= 0)
    {
        unsigned int rev = 0;
        unsigned int rm = 0;

        //copy of original number
        unsigned int tmp = N;

        while(tmp != 0)
        {
            //Extract the last digit
            rm = tmp% 10;

            //Appending last digit
            rev = (rev*10)+rm;

            //Remove the last digit from the number:
            tmp /=10;
        }

        //check whether palindrome number
        isPalindrome = (rev == N);
    }
    return isPalindrome;
}


int main()
{
    int N = 121;
    const bool isPalindrome = checkPalindrome(N);
    if (isPalindrome)
    {
        cout << "Palindrome Number" << endl;
    }
    else
    {
        cout << "Not Palindrome Number" << endl;
    }

    return 0;
}

Output:

Palindrome Number

Time Complexity: O(logN) for reversing N digits of input integer.

Space Complexity: O(1)

 

Recommended Articles for you: