0
0
CppProgramBeginner · 2 min read

C++ Program to Check Palindrome Number

A C++ program to check palindrome number reverses the number using a loop and compares it with the original number using code like if (original == reversed) { /* palindrome */ }.
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, think of reading it backwards and forwards. Reverse the digits one by one by taking the last digit using % 10 and building a new number. Then compare the reversed number with the original. If they are the same, the number is a palindrome.
📐

Algorithm

1
Get the input number and store it as original.
2
Initialize reversed number to 0.
3
Extract last digit of the number using modulo 10.
4
Add this digit to reversed number after shifting reversed number by one digit.
5
Remove last digit from the number by dividing by 10.
6
Repeat steps 3-5 until the number becomes 0.
7
Compare reversed number with the stored original number.
8
If equal, print palindrome message; otherwise, print not palindrome.
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int num, original, reversed = 0, digit;
    cout << "Enter a number: ";
    cin >> num;
    original = num;
    while (num > 0) {
        digit = num % 10;
        reversed = reversed * 10 + digit;
        num /= 10;
    }
    if (original == reversed)
        cout << original << " is a palindrome number." << endl;
    else
        cout << original << " is not a palindrome number." << endl;
    return 0;
}
Output
Enter a number: 121 121 is a palindrome number.
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks palindrome.

1

Initialize variables

num = 121, original = 121, reversed = 0

2

First loop iteration

digit = 121 % 10 = 1; reversed = 0 * 10 + 1 = 1; num = 121 / 10 = 12

3

Second loop iteration

digit = 12 % 10 = 2; reversed = 1 * 10 + 2 = 12; num = 12 / 10 = 1

4

Third loop iteration

digit = 1 % 10 = 1; reversed = 12 * 10 + 1 = 121; num = 1 / 10 = 0

5

Compare original and reversed

original = 121, reversed = 121; they are equal, so palindrome

numdigitreversed
12111
12212
11121
💡

Why This Works

Step 1: Extract digits

Using num % 10 extracts the last digit of the number to build the reversed number.

Step 2: Build reversed number

Multiply reversed by 10 to shift digits left and add the extracted digit to form the reversed number.

Step 3: Compare original and reversed

If the reversed number equals the original, the number reads the same backward and forward, so it is a palindrome.

🔄

Alternative Approaches

String conversion
cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string num, rev;
    cout << "Enter a number: ";
    cin >> num;
    rev = string(num.rbegin(), num.rend());
    if (num == rev)
        cout << num << " is a palindrome number." << endl;
    else
        cout << num << " is not a palindrome number." << endl;
    return 0;
}
This method uses string reversal which is simpler but uses extra memory for strings.
Recursive check
cpp
#include <iostream>
using namespace std;

bool isPalindrome(int num, int& rev) {
    if (num == 0) return true;
    if (!isPalindrome(num / 10, rev)) return false;
    rev = rev * 10 + num % 10;
    return true;
}

int main() {
    int num, rev = 0, original;
    cout << "Enter a number: ";
    cin >> num;
    original = num;
    isPalindrome(num, rev);
    if (original == rev)
        cout << original << " is a palindrome number." << endl;
    else
        cout << original << " is not a palindrome number." << endl;
    return 0;
}
Uses recursion to build reversed number but is more complex and less efficient.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once per digit of the number, so time is proportional to the number of digits, O(d).

Space Complexity

Only a few integer variables are used, so space is constant, O(1).

Which Approach is Fastest?

The numeric reversal method is fastest and uses least memory compared to string conversion or recursion.

ApproachTimeSpaceBest For
Numeric reversalO(d)O(1)Fastest and memory efficient
String conversionO(d)O(d)Simple code, uses extra memory
Recursive reversalO(d)O(d)Demonstrates recursion, less efficient
💡
Always store the original number before reversing it to compare later.
⚠️
Forgetting to reset or store the original number before modifying it in the loop.