Java Program to Check Palindrome Number

A palindrome number is a number that remains the same when its digits are reversed. For example, 121 and 1331 are palindrome numbers.

In this tutorial, we will create a Java program to check whether a given number is a palindrome using loops and basic arithmetic operations.

By the end of this guide, you will understand how number reversal logic can be used in conditional checking.

Concept Overview

To check a palindrome number, we reverse the original number and compare it with the original value.

If both are equal, the number is a palindrome; otherwise, it is not.

Program

Java
import java.util.Scanner;

public class PalindromeNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        int original = num;
        int reversed = 0;

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

        if (original == reversed) {
            System.out.println(original + " is a Palindrome Number");
        } else {
            System.out.println(original + " is not a Palindrome Number");
        }
    }
}

Output

TEXT
Enter a number: 121
121 is a Palindrome Number

Detailed Explanation

The program starts by taking input from the user using the Scanner class.

The original number is stored in a separate variable before modification.

The number is then reversed using a while loop and arithmetic operations.

After reversing, the program compares the reversed number with the original number.

If both values match, the number is identified as a palindrome.

Example Walkthrough

Let us consider the input 121.

Reversing 121 gives 121.

Since both are equal, it is a palindrome number.

Applications

Palindrome logic is used in data validation, string processing, and algorithm design.

It is also useful in computer science problems involving symmetry and pattern matching.

Advantages of This Approach

This program helps beginners understand loops, conditions, and number manipulation.

It demonstrates how to reuse reversal logic for problem-solving.

Limitations

The program only works with integers and does not handle negative numbers or strings.

Improvements You Can Make

You can extend this program to check palindrome strings.

You can also handle negative numbers and ignore special cases.

Using methods can improve code reusability and structure.

This Java program helps build strong logical thinking skills and strengthens understanding of loops and conditional statements.