Check Palindrome Number in Java

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit reversal

What you’ll learn

  • What a palindrome number means (same forward and backward).
  • How to reverse digits using % 10 and / 10 in Java.
  • How to check one number and print palindromes in a range.

Overview

A number is palindrome when its digits read the same from both ends. We reverse digits using arithmetic and compare with the original value.

Prerequisites

Integer arithmetic, modulo/division by 10, and loops.

  • Use % 10 to get the last digit and / 10 to remove it.
  • Basic Java control flow with while loops and comparisons.

Core idea

Reverse the digits and compare the reversed value with the original number.

Live preview

Live result
Press "Check palindrome".

Algorithm

Build reversed from last digits and test original == reversed.

📜 Pseudocode

Pseudocode
original = n
reversed = 0
while n != 0:
  reversed = reversed * 10 + (n % 10)
  n = n / 10
return original == reversed
1

Check one number

java
public class Main {
    static boolean isPalindrome(int number) {
        int original = number;
        int reversed = 0;
        while (number != 0) {
            int digit = number % 10;
            reversed = reversed * 10 + digit;
            number /= 10;
        }
        return original == reversed;
    }

    public static void main(String[] args) {
        int number = 121;
        if (isPalindrome(number)) {
            System.out.println(number + " is a palindrome number.");
        } else {
            System.out.println(number + " is not a palindrome number.");
        }
    }
}
2

Palindromes from 100 to 200

java
public class Main {
    static boolean isPalindrome(int number) {
        int original = number;
        int reversed = 0;
        while (number != 0) {
            int digit = number % 10;
            reversed = reversed * 10 + digit;
            number /= 10;
        }
        return original == reversed;
    }

    public static void main(String[] args) {
        System.out.println("Palindrome numbers in the range 100 to 200:");
        for (int i = 100; i <= 200; i++) {
            if (isPalindrome(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}

Optimization

For simple interviews, full reverse is enough. Advanced variant compares half-digits to reduce overflow risk.

❓ FAQ

A number that reads the same forward and backward, like 121 or 9009.
Reversing creates the backward form for direct comparison with the original.
Yes. Zero reads the same both ways.
Yes, all single-digit nonnegative integers are palindrome numbers.
This tutorial uses nonnegative integers only.
Checking one number takes O(d) where d is number of digits.

🔄 Input / output examples

121 -> palindrome, 123 -> not palindrome, 0 -> palindrome.

Edge cases

Negative values are treated as non-palindromes in this tutorial scope; watch integer overflow if reversing very large numbers.

⏱️ Time and space complexity

Single check: O(d) where d is number of digits, extra space O(1).

Summary

  • Reverse digits and compare to original.
  • Complexity is linear in digit count.
Did you know?

Single-digit numbers like 7 are always palindrome numbers because they read the same in both directions.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful