Palindrome Check

Program to check if a number is palindrome

BeginnerTopic: Loop Programs
Back

C++ Palindrome Check Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

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

Understanding Palindrome Check

This program checks whether a number is a palindrome. A palindrome number reads the same forwards and backwards. For example, 121 reads as "121" from both directions. This program demonstrates number reversal, comparison logic, and is fundamental for string manipulation and number theory problems.

---

1. What is a Palindrome Number?

A palindrome number is a number that remains the same when its digits are reversed.

Examples of palindrome numbers:

-

121

→ reversed is

121

-

1331

→ reversed is

1331

-

7

→ reversed is

7

✅ (single digit)

-

1221

→ reversed is

1221

Examples of non-palindrome numbers:

-

123

→ reversed is

321

-

456

→ reversed is

654

-

10

→ reversed is

01

(which is 1) ❌

Applications:

String palindrome checking
Number pattern problems
Algorithm design
Competitive programming

---

2. Header File: #include <iostream>

#include <iostream>

Provides:

cout → for displaying output
cin → for reading input

---

3. Declaring Variables

int num, reversed = 0, remainder, original;

Variable `num`:

Stores the number entered by the user
Will be modified during reversal process

Variable `reversed`:

Stores the reversed number we're building

-

Initialized to 0

starting point for building reversed number

Variable `remainder`:

Temporarily stores the last digit extracted from num
Used in the reversal process

Variable `original`:

Saves the original number before modification
Needed for comparison and display

Why save original?

num will be reduced to 0 during reversal
We need original value to compare with reversed number
Also needed for output message

---

4. Taking Input From User

`cout << "Enter a number: ";`

cin >> num;

Prompts user to enter a number
Reads and stores it in num

Example:

User enters:

121

num = 121

---

5. Saving Original Number

original = num;

Why this is crucial:

num will be modified during reversal
We need the original value to:
1.Compare with reversed number
2.Display in the output message

If we don't save it:

After reversal: num = 0, reversed = 121
We can't compare or display the original number!

---

6. Reversing the Number

while (num != 0)

remainder = num % 10;

reversed = reversed * 10 + remainder;

num /= 10;

This is the same reversal algorithm from the "Reverse a Number" program.

Step-by-step (for num = 121):

Iteration 1:

Extract: remainder = 121 % 10 = 1
Build: reversed = 0 * 10 + 1 = 1
Remove: num = 121 / 10 = 12
Now: reversed = 1, num = 12

Iteration 2:

Extract: remainder = 12 % 10 = 2
Build: reversed = 1 * 10 + 2 = 12
Remove: num = 12 / 10 = 1
Now: reversed = 12, num = 1

Iteration 3:

Extract: remainder = 1 % 10 = 1
Build: reversed = 12 * 10 + 1 = 121
Remove: num = 1 / 10 = 0
Now: reversed = 121, num = 0

Iteration 4:

Check: num != 00 != 0

false

-

Loop stops

Result:

reversed = 121

---

7. Comparing Original with Reversed

if (original == reversed)

This is the key check:

If original number equals reversed number →

Palindrome

If they don't match →

Not a palindrome

For our example:

original = 121
reversed = 121
121 == 121

true

Palindrome

Example with non-palindrome:

Input:

123

After reversal: reversed = 321
123 == 321

false

Not a palindrome

---

8. Displaying the Result

If palindrome:

cout << original << " is a palindrome" << endl;

If not palindrome:

cout << original << " is not a palindrome" << endl;

Examples:

Input: 121 → Output:

121 is a palindrome

Input: 123 → Output:

123 is not a palindrome

---

9. Complete Example Walkthrough

Example 1: Palindrome (121)

Step 1:

Input

User enters:

121

num = 121, original = 121

Step 2:

Reverse

Iteration 1: Extract 1, Build 1, Remove → num = 12, reversed = 1
Iteration 2: Extract 2, Build 12, Remove → num = 1, reversed = 12
Iteration 3: Extract 1, Build 121, Remove → num = 0, reversed = 121

Step 3:

Compare

original (121) == reversed (121)

true

Step 4:

Output

-

"121 is a palindrome"

Example 2: Not a Palindrome (123)

Step 1:

Input

num = 123, original = 123

Step 2:

Reverse

After reversal: reversed = 321

Step 3:

Compare

123 == 321

false

Step 4:

Output

-

"123 is not a palindrome"

---

10. Special Cases

Case 1: Single digit numbers

All single digit numbers (0-9) are palindromes
Example: 7 → reversed is 7 →

Palindrome

Case 2: Two-digit palindromes

Only numbers where both digits are same
Examples: 11, 22, 33, 44, ..., 99
12 → reversed is 21 →

Not palindrome

Case 3: Numbers with leading zeros

Input: 100 → reversed is 001 (which is 1)
100 != 1 →

Not palindrome

Note: Leading zeros are not displayed in integers

Case 4: Negative numbers

This program doesn't handle negatives
Could add: if (num < 0) { /* handle negative */ }

---

11. Why This Algorithm Works

The key insight:

Palindrome means: number = reverse(number)
We reverse the number and compare
If equal → palindrome, else → not palindrome

Mathematical perspective:

Original: 121 = 1×100 + 2×10 + 1×1
Reversed: 121 = 1×100 + 2×10 + 1×1
They're the same! ✅

---

12. Extension: String Palindromes

The same concept applies to strings:

"madam" → reversed is "madam" →

Palindrome

"hello" → reversed is "olleh" →

Not palindrome

String palindrome checking is similar but uses character comparison instead of digit manipulation.

---

Summary

Palindrome number reads the same forwards and backwards
Algorithm: Reverse the number, then compare with original
Save original number before reversal (crucial!)
If original == reversed → palindrome, else → not palindrome
This pattern is used in many string and number manipulation problems

This program teaches:

Number reversal technique
Comparison logic
Saving original values before modification
Pattern recognition (palindrome property)

Understanding palindromes helps in:

String manipulation problems
Number pattern recognition
Algorithm design
Many competitive programming challenges

Palindromes are a fundamental concept in programming, and this program demonstrates an elegant way to check for them using number reversal and comparison.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Palindrome Check

This C++ program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents