Palindrome Check

Program to check if a number is palindrome

C++Beginner
C++
#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

Palindrome Check in C++

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.

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) ❌

Algorithm

  1. Save the original number
  2. Reverse the number using the reversal algorithm
  3. Compare original with reversed
  4. If equal → palindrome, else → not palindrome

The Key Check

if (original == reversed)

This is the key check:

  • If original number equals reversed number → ## Palindrome ✅
  • If they don't match → ## Not a palindrome ❌

For example:

  • original = 121
  • reversed = 121
  • 121 == 121 → ## true → ## Palindrome ✅

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