Palindrome Check
Program to check if a number is palindrome
C++ Palindrome Check Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
---
2. Header File: #include <iostream>
#include <iostream>
Provides:
cout → for displaying outputcin → for reading input---
3. Declaring Variables
int num, reversed = 0, remainder, original;
Variable `num`:
Variable `reversed`:
-
Initialized to 0
Variable `remainder`:
numVariable `original`:
Why save original?
num will be reduced to 0 during reversal---
4. Taking Input From User
cin >> num;
numExample:
121
num = 121---
5. Saving Original Number
original = num;
Why this is crucial:
num will be modified during reversalIf we don't save it:
num = 0, reversed = 121---
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:
remainder = 121 % 10 = 1reversed = 0 * 10 + 1 = 1num = 121 / 10 = 12reversed = 1, num = 12Iteration 2:
remainder = 12 % 10 = 2reversed = 1 * 10 + 2 = 12num = 12 / 10 = 1reversed = 12, num = 1Iteration 3:
remainder = 1 % 10 = 1reversed = 12 * 10 + 1 = 121num = 1 / 10 = 0reversed = 121, num = 0Iteration 4:
num != 0 → 0 != 0 →false
-
Loop stops
Result:
reversed = 121 ✅
---
7. Comparing Original with Reversed
if (original == reversed)
This is the key check:
Palindrome
✅
Not a palindrome
❌
For our example:
original = 121reversed = 121121 == 121 →true
→
Palindrome
✅
Example with non-palindrome:
123
reversed = 321123 == 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:
121 is a palindrome
123 is not a palindrome
---
9. Complete Example Walkthrough
Example 1: Palindrome (121)
Step 1:
Input
121
num = 121, original = 121Step 2:
Reverse
num = 12, reversed = 1num = 1, reversed = 12num = 0, reversed = 121Step 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 = 123Step 2:
Reverse
reversed = 321Step 3:
Compare
123 == 321 →false
Step 4:
Output
-
"123 is not a palindrome"
❌
---
10. Special Cases
Case 1: Single digit numbers
Palindrome
✅
Case 2: Two-digit palindromes
Not palindrome
❌
Case 3: Numbers with leading zeros
Not palindrome
❌
Case 4: Negative numbers
if (num < 0) { /* handle negative */ }---
11. Why This Algorithm Works
The key insight:
Mathematical perspective:
---
12. Extension: String Palindromes
The same concept applies to strings:
Palindrome
Not palindrome
String palindrome checking is similar but uses character comparison instead of digit manipulation.
---
Summary
This program teaches:
Understanding palindromes helps in:
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.