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

12/04/2024 0 By indiafreenotes

Palindrome is a sequence that reads the same backward as forward. Palindrome checking is a classic problem in computer science and programming education, illustrating basic concepts such as loops, conditionals, and the manipulation of numbers or strings. In the case of numeric palindromes, the problem typically involves reversing a number and comparing it to the original to determine if they are the same.

This task can be approached in various ways, but one straightforward method involves reversing the given number and then comparing the reversed number with the original number. If they are identical, the number is a palindrome.

C++ Program to Check Numeric Palindrome

Here is a simple C++ program that checks if a given integer is a palindrome. The program outlines the process of reversing a number and comparing the reversed number with the original.

#include <iostream>

using namespace std;

// Function to reverse a number

int reverseNumber(int num) {

    int reversed = 0;

    while(num > 0) {

        reversed = reversed * 10 + num % 10;

        num /= 10;


    return reversed;


// Function to check if a number is a palindrome

bool isPalindrome(int num) {

    // Negative numbers are not considered palindromes

    if (num < 0) return false;


    // Compare original number with its reverse

    return num == reverseNumber(num);


int main() {

    int num;

    cout << “Enter a number: “;

    cin >> num;


    if (isPalindrome(num)) {

        cout << num << ” is a palindrome.” << endl;

    } else {

        cout << num << ” is not a palindrome.” << endl;



    return 0;




  • Reversing a Number:

The reverseNumber function reverses the digits of the given number. It initializes an integer reversed to 0 and iteratively extracts the last digit of num (using num % 10), appends it to reversed, and removes the last digit from num (using num /= 10) until num becomes 0.

  • Checking for Palindrome:

The isPalindrome function first handles a special case by returning false if the number is negative, as negative numbers are not considered palindromes due to the leading minus sign. It then calls reverseNumber to reverse the input number and compares the reversed number with the original. If they are the same, it returns true, indicating the number is a palindrome; otherwise, it returns false.

  • Main Program Flow:

The main function asks the user to enter a number, checks if it’s a palindrome by calling isPalindrome, and outputs the result.