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

Updated on December 13, 2024
Check whether a number is palindrome or not header image

Introduction

A palindrome is a sequence that reads the same backward as forward. Commonly known as symmetric sequences, palindromes are frequently highlighted in numbers, words, and phrases. In the context of programming, determining if a number is a palindrome involves reversing its digits and comparing the result to the original number. This concept is often applied in computational problems to practice logic building and algorithm design.

In this article, you will learn how to create a C++ program that checks whether a given number is a palindrome. Explore practical examples that illustrate the process step-by-step and enhance your understanding of control structures, loops, and other fundamental aspects of C++ programming.

Setting Up the Code Structure

Before diving into writing the function to check for a palindrome, understand the structure of a basic C++ program which includes necessary libraries and using the standard namespace.

  1. Initiate by including the Input/Output stream header file.

  2. Declare the main() function where the execution of the program begins.

    cpp
    #include <iostream>
    using namespace std;
    
    int main() {
        // Program code will go here
        return 0;
    }
    

    This snippet sets up the basic C++ program structure, incorporating the standard header for handling input and output operations, and declares the main() function.

Designing the Palindrome Check Function

To check if a number is a palindrome, create a function isPalindrome that returns true if the number is a palindrome and false otherwise. This function will reverse the given number and compare it with the original number.

  1. Define the function isPalindrome that takes an integer as a parameter.

  2. Implement the logic to reverse the digits of the number.

  3. Compare the reversed number with the original number to determine if it's a palindrome.

    cpp
    bool isPalindrome(int num) {
        int original = num;
        int reversed = 0;
        while (num > 0) {
            int digit = num % 10;
            reversed = reversed * 10 + digit;
            num /= 10;
        }
        return original == reversed;
    }
    

    This function first stores the original number. Then, it extracts each digit from the number, appends it to reversed, and removes it from num. Finally, it checks if the reversed number is the same as the original.

Integrating the Palindrome Function in Main

Now integrate the isPalindrome function within the main() function to allow user interaction and dynamic checking.

  1. Prompt the user to enter a number.

  2. Call the isPalindrome function and pass the user's number.

  3. Display the result based on the function's return value.

    cpp
    int main() {
        int num;
        cout << "Enter a number to check if it's a palindrome: ";
        cin >> num;
        bool result = isPalindrome(num);
        if (result) {
            cout << num << " is a palindrome." << endl;
        } else {
            cout << num << " is not a palindrome." << endl;
        }
        return 0;
    }
    

    Here, the program reads an integer input from the user and checks whether it is a palindrome using the isPalindrome function. It then outputs the appropriate message based on the result.

Conclusion

C++ offers a robust platform for practicing and implementing basic to complex algorithmic problems, such as checking whether a number is a palindrome. Through this exercise, you've seen how to manipulate numbers, implement functions, and use conditional logic in C++. This example not only strengthens understanding of fundamental programming concepts but also equips you with practical skills to approach similar problems. Implementing techniques covered in this article ensures your foundation in C++ remains solid, and you are better prepared for more challenging programming tasks ahead.