C++ Program to Check Palindrome Number

C++ Program to Check Palindrome Number

Palindrome numbers are numbers that read the same backward as forward, such as 121 or 454. They are commonly used in programming exercises to teach concepts like loops, conditionals, and number manipulation. In C++, checking whether a number is a palindrome is a simple yet important exercise that helps beginners understand control flow and problem-solving techniques.

Program 1: Basic Method to Check Palindrome Number

This program checks if a number is a palindrome by reversing it and comparing it to the original number. It uses simple arithmetic operations without converting the number into a string.

#include <iostream>

using namespace std;

int main() {

    int n, original, reversed = 0, remainder;

    cout << "Enter a number: " << endl;
    cin >> n;

    original = n;

    while(n != 0) {

        remainder = n % 10;
        reversed = reversed * 10 + remainder;
        n /= 10;

    }

    if(original == reversed)
        cout << original << " is a palindrome number." << endl;

    else
        cout << original << " is not a palindrome number." << endl;

    return 0;

}

In this program, we extract each digit using the modulo operator and build the reversed number. Finally, we compare the reversed number with the original input. This method is easy for beginners to understand because it directly applies mathematical operations to solve the problem.

Program 2: Using a Function

Encapsulating the palindrome check in a function makes the code modular and reusable. This is helpful when you need to check multiple numbers without rewriting the logic.

#include <iostream>

using namespace std;

bool isPalindrome(int n) {

    int original = n, reversed = 0, remainder;

    while(n != 0) {

        remainder = n % 10;
        reversed = reversed * 10 + remainder;
        n /= 10;

    }

    return original == reversed;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isPalindrome(n))
        cout << n << " is a palindrome number." << endl;

    else
        cout << n << " is not a palindrome number." << endl;

    return 0;

}

This version introduces the isPalindrome() function, which returns a boolean value. Using functions makes the code cleaner and easier to maintain. Beginners also learn the benefits of reusability and modular programming.

Program 3: Checking Palindrome Using Strings

Another approach is to convert the number into a string and use two pointers to compare characters from both ends. This method simplifies the logic and is easy to understand for those familiar with strings.

#include <iostream>
#include <string>

using namespace std;

bool isPalindromeString(int n) {

    string str = to_string(n);
    int start = 0, end = str.length() - 1;

    while(start < end) {

        if(str[start] != str[end])
            return false;

        start++;
        end--;

    }

    return true;

}

int main() {

    int n;

    cout << "Enter a number: " << endl;
    cin >> n;

    if(isPalindromeString(n))
        cout << n << " is a palindrome number." << endl;

    else
        cout << n << " is not a palindrome number." << endl;

    return 0;

}

This string-based approach is intuitive because it allows direct comparison of characters. Beginners can quickly see how loops and conditionals work together to solve problems efficiently.

Program 4: Check Palindrome Numbers in a Range

Sometimes, you may want to find all palindrome numbers within a certain range. This program iterates through a range of numbers and prints each number that is a palindrome. It demonstrates how functions can be reused and how loops can work over multiple values efficiently.

#include <iostream>

using namespace std;

bool isPalindrome(int n) {

    int original = n, reversed = 0, remainder;

    while(n != 0) {

        remainder = n % 10;
        reversed = reversed * 10 + remainder;
        n /= 10;

    }

    return original == reversed;

}

int main() {

    int start, end;

    cout << "Enter the starting number of the range: " << endl;
    cin >> start;

    cout << "Enter the ending number of the range: " << endl;
    cin >> end;

    cout << "Palindrome numbers between " << start << " and " << end << " are: ";

    for(int i = start; i <= end; i++) {

        if(isPalindrome(i))
            cout << i << " ";

    }

    cout << endl;

    return 0;

}

In this program, the isPalindrome() function is reused for every number in the specified range. The for loop checks each number, and only those that satisfy the palindrome condition are printed. Beginners can see how modular code and loops work together to solve larger problems efficiently. This approach is especially useful when analyzing patterns in numbers or performing batch checks.

Frequently Asked Questions (FAQ)

Here are some common questions beginners ask about palindrome numbers in C++:

Q1: Can negative numbers be palindromes?
No, negative numbers are not considered palindromes because of the negative sign.

Q2: Which method is faster for large numbers?
The arithmetic-based method is generally faster and uses less memory compared to the string-based approach.

Q3: Can I check multiple numbers using a function?
Yes, encapsulating the logic in a function allows you to reuse it for multiple numbers efficiently.

Q4: Are palindrome numbers only for integers?
No, you can also check for palindromic strings or sequences in text and arrays.

Conclusion

Checking palindrome numbers in C++ is a great way to learn about loops, conditionals, and functions. We explored three methods: a basic arithmetic approach, a modular function, and a string-based method. Beginners can start with the simple method to understand the logic, then move on to functions or strings for cleaner and more reusable code. Practicing these techniques strengthens both programming skills and logical thinking.

Additional & References

Understanding palindromes helps with number theory, algorithms, and coding challenges. Beginners are encouraged to try checking multiple numbers, generating palindromes within a range, and exploring optimizations.

Scroll to Top