C++ Program to Check Palindrome Number
if (original == reversed) { /* palindrome */ }.Examples
How to Think About It
% 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
Code
#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; }
Dry Run
Let's trace the number 121 through the code to see how it checks palindrome.
Initialize variables
num = 121, original = 121, reversed = 0
First loop iteration
digit = 121 % 10 = 1; reversed = 0 * 10 + 1 = 1; num = 121 / 10 = 12
Second loop iteration
digit = 12 % 10 = 2; reversed = 1 * 10 + 2 = 12; num = 12 / 10 = 1
Third loop iteration
digit = 1 % 10 = 1; reversed = 12 * 10 + 1 = 121; num = 1 / 10 = 0
Compare original and reversed
original = 121, reversed = 121; they are equal, so palindrome
| num | digit | reversed |
|---|---|---|
| 121 | 1 | 1 |
| 12 | 2 | 12 |
| 1 | 1 | 121 |
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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Numeric reversal | O(d) | O(1) | Fastest and memory efficient |
| String conversion | O(d) | O(d) | Simple code, uses extra memory |
| Recursive reversal | O(d) | O(d) | Demonstrates recursion, less efficient |