beginnerNumber ProblemsJava

Check Whether a Number Is a Palindrome

Determine whether an integer's digits read the same forwards and backwards.

Quick Answer

Reverse the number digit by digit using result = result * 10 + number % 10 while dividing the number by 10, then compare the reversed value with the original. If they are equal, the number is a palindrome. Keep a copy of the original before you start reversing.

Problem Statement

Given a non-negative integer, decide whether it is a palindrome — that is, whether its digits read the same from left to right as from right to left. For example, 121 and 1331 are palindromes, while 123 is not.

Return true if the number is a palindrome and false otherwise. Solve it with arithmetic (no String conversion): reverse the digits and compare the reversed number with the original.

Input: A single non-negative integer n.

Output: true if n is a palindrome, otherwise false.

Examples

Example 1
Input:  121
Output: true

Reversing 121 gives 121, which equals the original, so it is a palindrome.

Example 2
Input:  123
Output: false

Reversing 123 gives 321, which differs from 123, so it is not a palindrome.

Constraints

  • 0 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • Negative numbers are treated as not palindromes because of the leading minus sign

Think Before You Code

Reveal the questions to ask yourself first
  • How do you reverse a number using only % 10 and / 10?
  • Why must you store the original value before you start reversing it?
  • Should a negative number ever count as a palindrome?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Reversing the number and comparing it with the original is the whole idea — but you must save the original first.
Hint 2
Build the reversed value with `reversed = reversed * 10 + n % 10`, then shrink `n` with `n = n / 10`.
Hint 3
Loop while `n != 0`, comparing `reversed` to the saved original at the end. Guard against negatives up front.

Approach

Reveal the step-by-step approach

Reverse the digits arithmetically, then compare with the original number.

  1. If n is negative, return false (the sign breaks symmetry).
  2. Save the original value in a separate variable, because the loop will destroy n.
  3. Set reversed = 0.
  4. While n is not 0:
    • digit = n % 10 — the current last digit.
    • reversed = reversed * 10 + digit — append it to the reversed number.
    • n = n / 10 — drop the digit you just used.
  5. Return whether reversed equals the saved original.

Dry Run

Walk through the example step by step

Checking n = 121:

step | n    | digit = n%10 | reversed = reversed*10 + digit
-----+------+--------------+-------------------------------
1    | 121  | 1            | 0*10 + 1   = 1
2    | 12   | 2            | 1*10 + 2   = 12
3    | 1    | 1            | 12*10 + 1  = 121
end  | 0    | —            | 121

reversed (121) == original (121) -> true.

Solution

Reveal the full Java solution
public class NumberPalindrome {
    public static boolean isPalindrome(int n) {
        if (n < 0) {
            return false;
        }
        int original = n;
        int reversed = 0;
        while (n != 0) {
            int digit = n % 10;
            reversed = reversed * 10 + digit;
            n /= 10;
        }
        return reversed == original;
    }

    public static void main(String[] args) {
        System.out.println(isPalindrome(121)); // true
        System.out.println(isPalindrome(123)); // false
    }
}

Because the loop repeatedly divides n down to 0, you must copy it into original before starting; otherwise you would have nothing to compare against. Reversing and comparing is enough for palindrome detection — you never need to convert the number to text.

Time: O(d) where d is the number of digits (log10 n)Space: O(1)

Common Mistakes

  • Reversing n without first saving the original, so there is nothing to compare against.
  • Treating negative numbers as palindromes; -121 reversed is not -121 because of the sign.

Edge Cases to Test

  • Single-digit numbers (0-9) are always palindromes.
  • Numbers ending in 0, like 10, are not palindromes (10 reversed is 1).

Interview Follow-Ups

  • How would you check palindromes by comparing only the first and second halves of the digits?
  • Can you detect a palindrome without building the full reversed number to avoid overflow?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us