Home / Programs / Write a java program to check whether the given number is palindrome or not.
Programming Example

Write a java program to check whether the given number is palindrome or not.

👁 16 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

Enter a number: 121

Expected Output:

The number is a Palindrome.

Program Code

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 reverse = 0;

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

        if (original == reverse) {
            System.out.println("The number is a Palindrome.");
        } else {
            System.out.println("The number is NOT a Palindrome.");
        }

        sc.close();
    }
}

Explanation

Logic Used

  • Store the original number.

  • Reverse the number using a while loop.

  • Compare original number with reversed number.

  • If both are equal → Palindrome

  • Otherwise → Not Palindrome

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.