← Back to libraryQuestion 74 of 273
⌨️Java CodingBeginner

Check if a Number is a Palindrome

📌 Definition:

Determine whether an integer reads the same forwards and backwards (e.g. 121 -> true, 123 -> false).

📖 Detailed Explanation:

Reverse the number arithmetically (or half of it) and compare to the original. Negative numbers are not palindromes (the minus sign). You can avoid string conversion entirely by reversing digits with %10 and /10.

💻 Solutions:
public static boolean isPalindrome(int n) {
    if (n < 0) return false;
    int original = n, rev = 0;
    while (n != 0) { rev = rev * 10 + n % 10; n /= 10; }
    return rev == original;
}
function isPalindrome(n) {
  if (n < 0) return false;
  const s = String(n);
  return s === s.split('').reverse().join('');
}
def is_palindrome(n):
    if n < 0:
        return False
    return str(n) == str(n)[::-1]
🔑 Key Points:
  • Negatives are not palindromes
  • Reverse digits with %10 // /10, compare
  • String reverse is a valid alternative
  • Reversing only half avoids overflow
🌍 Real-World Example:

A quick check that combines integer math with an edge case (negatives) — the kind of small trap testers are wired to catch.

🎯 Scenario-Based Interview Question:

Should -121 be considered a palindrome?