Palindrome Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

  

C Program

#include<stdio.h>

int main() {
    int n, r = 0, t;
    scanf("%d", &n);
    t = n;
    while (n) {
        r = r * 10 + n % 10;
        n /= 10;
    }
    printf(t == r ? "Palindrome" : "Not Palindrome");
}

C Output

Input: 121  
Output: Palindrome


C++ Program

#include<iostream>
using namespace std;

int main() {
    int n, r = 0, t;
    cin >> n;
    t = n;
    while (n) {
        r = r * 10 + n % 10;
        n /= 10;
    }
    cout << (t == r ? "Palindrome" : "Not Palindrome");
}

C++ Output

Input: 123  
Output: Not Palindrome


JAVA Program

import java.util.*;

class P {
    public static void main(String[] a) {
        int n = new Scanner(System.in).nextInt(), r = 0, t = n;
        while (n > 0) {
            r = r * 10 + n % 10;
            n /= 10;
        }
        System.out.print(t == r ? "Palindrome" : "Not Palindrome");
    }
}

JAVA Output

Input: 1331  
Output: Palindrome


Python Program

n = int(input())
print("Palindrome" if str(n) == str(n)[::-1] else "Not Palindrome")

Python Output

Input: 12321  
Output: Palindrome


In-Depth Learning – Complete Concept in Paragraphs
What Is a Palindrome Number?
A palindrome number is a number which is the same when its digits are reversed. That is, it "reads the same backward and forward." For instance, numbers 121, 1331, and 12321 are palindromes. Non-palindromes are numbers like 123, 1010, etc. This idea is used extensively in mathematical puzzles, logic-building problems, and interview problems.

How Does the Logic Work?
To determine whether a number is a palindrome, we reverse it and see if it equals the original. Here, if both are the same, then it's a palindrome. We loop to get each digit (with % 10), construct the reverse by taking the reversed number and multiplying it by 10 and adding the digit, and lastly check whether the reversed number is the same as the original.

Example
Suppose we have the number 121.

Original: 121

Step 1: r = 0 × 10 + 1 = 1, n = 12

Step 2: r = 1 × 10 + 2 = 12, n = 1

Step 3: r = 12 × 10 + 1 = 121, n = 0
Now compare original 121 with reverse 121. They are equal → Palindrome.

Now try 123:

Reverse = 321 ≠ 123 → Not a palindrome.

Real-Life Analogy
Imagine a palindrome as viewing a symmetrical drawing — when you fold the picture halfway down, both sides look alike. In the same way, when you reverse a number, if it "folds" back upon itself to form the original number, it's a palindrome. Another comparison is with a mirror image — when the reflection looks just like the object, you have symmetry, as with a palindrome in numbers.

Why Palindrome Check Is Useful
Palindrome checking is a great exercise in building logic. It helps students learn to reverse numbers, employ loops, compare values, and play with digits. Palindrome checks are applied in data validation, pattern matching, and even DNA sequence interpretation in bioinformatics in actual applications. They are also applied in competitive coding when quick validation logic is required.

Python-Specific Benefit
Python offers a very clean way to reverse a number by simply converting it into a string and using slicing ([::-1]). This reduces the code to a single line while maintaining full functionality, which is great for competitive coding or quick scripting.

SEO-Optimized Natural Paragraph for Ranking
If you're after the most efficient and most beginner-friendly solution to verify if a number is a palindrome, this tutorial has everything you require. With neat and concise code samples in C, C++, Java, and Python, this solution addresses both the concept and the coding. The use of palindrome numbers is frequently requested in interviews and examinations as a means to assess comprehension of loops, conditionals, and number manipulation. Whether you are preparing for a technical interview, doing basic logical practice, or simply learning how to reverse and compare numbers, knowing this problem in-depth provides you with a deeper sense of control flow and arithmetic operations in programming.