Last Updated: June 7, 2026
A palindrome is a sequence that reads the same in both directions. For numbers, that means the digits are mirror images around the center. The value 12321 reads identically left to right and right to left, so it qualifies. The value 12345 does not, because its reverse is 54321.
The most direct way to test this is to reverse the digits and compare the reversed value with the original. If they match, the number is a palindrome. Two details deserve attention before writing any code. The first is the sign. A negative number carries a minus sign on the left, and reversing the characters would push that sign to the right, so no negative number can ever read the same in both directions. The second is the trailing zero. A positive number that ends in 0, such as 10, can only be a palindrome if its first digit is also 0, and the only number that starts with 0 is 0 itself.
false for any n < 0.0 cannot be a palindrome, because its reverse would need a leading 0. The single exception is 0, which reads the same either way.The definition of a palindrome translates straight into an algorithm: reverse the number and check whether the reversed value equals the original. Reversing an integer mathematically means repeatedly peeling off the last digit with n % 10 and pushing it onto a growing reversed value with reversed * 10 + digit. After draining every digit, you compare the reversed result against the number you started with.
Negative numbers are ruled out immediately, since the sign alone guarantees they fail. One subtlety remains. The reversed value of a large 32-bit number can exceed the 32-bit range. Building it inside a 64-bit long keeps the arithmetic safe, after which a plain equality check finishes the job.
n is negative, return false.n so you can compare against it later.reversed accumulator to 0.n is greater than 0, take the last digit with n % 10, append it with reversed = reversed * 10 + digit, and drop the digit with n = n / 10.true if reversed equals the saved original, and false otherwise.Input:
The number 121 is not negative, so the work begins. Keep 121 as the original. Start with reversed = 0. First pass: the last digit is 121 % 10 = 1, so reversed becomes 0 * 10 + 1 = 1, and n becomes 12. Second pass: the last digit is 12 % 10 = 2, so reversed becomes 1 * 10 + 2 = 12, and n becomes 1. Third pass: the last digit is 1 % 10 = 1, so reversed becomes 12 * 10 + 1 = 121, and n becomes 0. The loop ends. The reversed value 121 equals the original 121, so the number is a palindrome.
d grows with the logarithm of n.Reversing the entire number does more work than necessary. To decide whether a number is a palindrome, you only need to compare its first half against its second half. So instead of reversing all the digits, you can peel digits off the back of the number and grow a reversed half until that reversed half catches up with the front part that remains. At that point the two halves carry enough information to give an answer, and there is no risk of overflow because the reversed half never holds more than half the digits.
A couple of guards come first. Negative numbers fail outright. Any positive number whose last digit is 0 also fails, because a leading 0 is impossible, with the lone exception of 0 itself.
After the loop, two cases arise. When the number has an even count of digits, the remaining front equals the reversed half exactly. When the count is odd, the reversed half holds one extra middle digit, so you drop it with revertedHalf / 10 before comparing. The middle digit sits on the axis of symmetry and does not affect the result.
n is negative, or if n ends in 0 while not being 0 itself, return false.revertedHalf to 0.n is greater than revertedHalf, move the last digit of n onto revertedHalf with revertedHalf = revertedHalf * 10 + n % 10, then drop that digit with n = n / 10.true if n equals revertedHalf (even digit count) or n equals revertedHalf / 10 (odd digit count, middle digit dropped).Input:
The number 121 is positive and does not end in 0, so the guards pass. Start with revertedHalf = 0. First pass: n is 121, which is greater than 0, so move the last digit. revertedHalf becomes 0 * 10 + 1 = 1, and n becomes 12. Second pass: n is 12, which is greater than 1, so move again. revertedHalf becomes 1 * 10 + 2 = 12, and n becomes 1. Now n is 1, which is not greater than revertedHalf of 12, so the loop stops. The digit count is odd, so compare n against revertedHalf / 10 = 12 / 10 = 1. Since n equals 1, the comparison succeeds and the number is a palindrome.