C++ Program to Check whether the given Number is Palindrome or not

January 17, 2023, Learn eTutorial

Here we are discussing a C++ program to check whether the given number is a palindrome or not.

What is a palindrome number?

A number that will remain the same when we reverse the digits in that number is called a palindrome number.

For example, 11,151,45254 are palindrome numbers.

check palindrome number

How can we check a given number is a palindrome?

Get the number to be checked and save the original copy into a variable ‘original’, to compare when the original number has been reversed. Then we separate the last digit of the number and add it to another variable ‘reverse’ in a loop. Now we check whether both numbers original and reverse are the same or not,  If both are the same then print ‘palindrome number’ and else print ‘not palindrome’.

How can we check for palindrome in C++?

Ask the user to enter a positive number and save it into another variable ‘n’ to compare when the original number has been reversed. By using a modulus operator we are separating the last digit of the number and save it in a variable. then add that digit to the reverse variable using the formula reverse = ( reverse * 10 ) + digit;. then we divide the number by 10 to remove the last digit and continue the loop until the original number is zero.

Finally, after the loop, the reversed number will be in the variable reverse. Now we have to compare the original number and the reversed number. 


Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare integer type variables; n, original, digit, reverse.

Step 5: Print a message to enter a positive number.

Step 6: Read the number into the variable original.

Step 7: store the original number into another variable n.

Step 8: Reverse the original number and store the reversed number in the variable reverse.

Step 9: Print the reversed number on the screen.

Step 10: compare the original number with the reversed number.

Step11: if n = reverse then print “The number is a palindrome”  else not palindrome

Step 12: Exit.

C++ Source Code

                                          #include <iostream>
using namespace std;

int main()
     int n, original, digit, reverse = 0;

     cout << "Enter a positive number: ";
     cin >> original;

     n = original;

         digit = original % 10;
         reverse = (reverse * 10) + digit;
         original = original / 10;
     } while (original != 0);

     cout << " The reverse of the number is: " << reverse << endl;

     if (n == reverse)
         cout << " The number is a palindrome.";
         cout << " The number is not a palindrome.";

    return 0;


Run 1
Enter a positive number: 12321
The reverse of the number is: 12321
 The number is a palindrome.
Run 2
Enter a positive number: 1452416
The reverse of the number is: 6142541
 The number is not a palindrome.