Java Program to Identify Palindrome Numbers Easily – Step-by-Step Guide

By Vandu
May 2, 2025

Follow us on


Learn how to check whether a number is a palindrome using Java with beginner-friendly steps. Includes explained code, logic, and real-world examples.

Java Program to Identify Palindrome Numbers Easily – Step-by-Step Guide

Check Whether a Number is a Palindrome in Java – Explained with Code

💡 What is a Palindrome Number?

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

  • 121 → reverse is 121 → ✅ Palindrome

  • 123 → reverse is 321 → ❌ Not a Palindrome

  • 1221 → reverse is 1221 → ✅ Palindrome


👨‍💻 How to Check Palindrome in Java?

To check whether a number is a palindrome:

  1. Reverse the number.

  2. Compare the reversed number with the original number.

  3. If both are the same, it's a palindrome.

Let’s break this logic down using Java step by step.


🧾 Java Code: Check if a Number is a Palindrome

import java.util.Scanner;

public class PalindromeChecker {

    public static void main(String[] args) {
        // Create Scanner object to take input from user
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int originalNumber = scanner.nextInt();

        int reversedNumber = 0;
        int temp = originalNumber;

        // Logic to reverse the number
        while (temp != 0) {
            int digit = temp % 10; // Get the last digit
            reversedNumber = reversedNumber * 10 + digit; // Append digit
            temp = temp / 10; // Remove last digit
        }

        // Check if the original number and reversed number are the same
        if (originalNumber == reversedNumber) {
            System.out.println(originalNumber + " is a palindrome number.");
        } else {
            System.out.println(originalNumber + " is not a palindrome number.");
        }

        // Close the scanner to avoid memory leak
        scanner.close();
    }
}

🧠 Step-by-Step Explanation (in Easy Words)

1️⃣ Import Scanner

We use Scanner to take input from the user.

import java.util.Scanner;

2️⃣ Get the User Input

We ask the user to enter a number. That number is stored in a variable called originalNumber.

int originalNumber = scanner.nextInt();

3️⃣ Reverse the Number

We use a loop to reverse the digits:

  • % (modulus) gives the last digit.

  • Multiply the reversed number by 10 and add the last digit.

  • Divide the original number by 10 to remove the last digit.

while (temp != 0) {
    int digit = temp % 10;
    reversedNumber = reversedNumber * 10 + digit;
    temp = temp / 10;
}
 

Example:
If input is 12321, loop runs like this:

  • digit = 1 → reversed = 1

  • digit = 2 → reversed = 12

  • digit = 3 → reversed = 123

  • digit = 2 → reversed = 1232

  • digit = 1 → reversed = 12321


4️⃣ Compare and Print Result

After reversing, we compare the original and reversed numbers.

if (originalNumber == reversedNumber)

If they are the same → it’s a palindrome. Otherwise → it’s not.


✅ Sample Output

Enter a number: 1221
1221 is a palindrome number.
Enter a number: 456
456 is not a palindrome number.

📌 Final Notes

  • Palindrome check works for any positive integer.

  • You can also modify the program to check palindrome for strings, like "madam" or "racecar".

  • This logic helps in many programming tasks like data validation, puzzle solving, or number games.


🔚 Conclusion

Now you know how to check whether a number is a palindrome in Java with a beginner-friendly approach. This is a simple and important concept for Java learners that also improves your problem-solving skills.


© 2025 Pay18News. All rights reserved.