Determine whether an integer reads the same forwards and backwards (e.g. 121 -> true, 123 -> false).
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.
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]A quick check that combines integer math with an edge case (negatives) — the kind of small trap testers are wired to catch.
Should -121 be considered a palindrome?