0
0
JavaProgramBeginner · 2 min read

Java Program to Check Palindrome Number

To check if a number is a palindrome in Java, reverse the number using while loop and compare it with the original number using if (original == reversed).
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, think of reading it from left to right and right to left. Reverse the digits of the number by extracting the last digit repeatedly and building a new number. Then compare this reversed number with the original number. If both are the same, the number is a palindrome.
📐

Algorithm

1
Get the input number.
2
Store the original number in a variable.
3
Initialize a variable to store the reversed number as 0.
4
Use a loop to extract the last digit of the number and add it to the reversed number.
5
Remove the last digit from the number by dividing it by 10.
6
After the loop, compare the reversed number with the original number.
7
If they are equal, print that the number is a palindrome; otherwise, print it is not.
💻

Code

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 /= 10;
        }

        if (original == reversed) {
            System.out.println(original + " is a palindrome number.");
        } else {
            System.out.println(original + " is not a palindrome number.");
        }
        sc.close();
    }
}
Output
Enter a number: 121 121 is a palindrome number.
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks for palindrome.

1

Initialize variables

original = 121, num = 121, reversed = 0

2

First loop iteration

digit = 121 % 10 = 1; reversed = 0 * 10 + 1 = 1; num = 121 / 10 = 12

3

Second loop iteration

digit = 12 % 10 = 2; reversed = 1 * 10 + 2 = 12; num = 12 / 10 = 1

4

Third loop iteration

digit = 1 % 10 = 1; reversed = 12 * 10 + 1 = 121; num = 1 / 10 = 0

5

Loop ends and compare

num = 0, reversed = 121, original = 121; since reversed == original, number is palindrome

numdigitreversed
12111
12212
11121
💡

Why This Works

Step 1: Extract digits

Using num % 10 extracts the last digit of the number to build the reversed number.

Step 2: Build reversed number

Multiply the current reversed number by 10 and add the extracted digit to shift digits left and append the new digit.

Step 3: Compare original and reversed

If the reversed number equals the original, the number reads the same forwards and backwards, so it is a palindrome.

🔄

Alternative Approaches

String reversal
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: ");
        String numStr = sc.nextLine();
        String reversedStr = new StringBuilder(numStr).reverse().toString();

        if (numStr.equals(reversedStr)) {
            System.out.println(numStr + " is a palindrome number.");
        } else {
            System.out.println(numStr + " is not a palindrome number.");
        }
        sc.close();
    }
}
This method uses string reversal which is simpler but uses extra memory for string objects.
Recursive check
java
public class PalindromeNumber {
    public static int isPalindrome(int num, int rev) {
        if (num == 0) return rev;
        return isPalindrome(num / 10, rev * 10 + num % 10);
    }

    public static void main(String[] args) {
        int num = 121;
        if (num == isPalindrome(num, 0)) {
            System.out.println(num + " is a palindrome number.");
        } else {
            System.out.println(num + " is not a palindrome number.");
        }
    }
}
This recursive method is elegant but may be harder to understand for beginners and uses call stack memory.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time complexity is O(d) where d is the number of digits.

Space Complexity

Only a few integer variables are used, so space complexity is O(1), constant space.

Which Approach is Fastest?

The numeric reversal approach is fastest and uses less memory than string reversal or recursion.

ApproachTimeSpaceBest For
Numeric reversalO(d)O(1)Efficient and memory-friendly
String reversalO(d)O(d)Simple to implement, uses extra memory
Recursive checkO(d)O(d)Elegant but uses call stack memory
💡
Always store the original number before reversing it to compare later.
⚠️
Forgetting to reset or store the original number before modifying it causes wrong palindrome checks.