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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.