VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-if-a-number-is-palindrome/

⇱ Check if a number is Palindrome - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if a number is Palindrome

Last Updated : 29 Mar, 2026

Given an integer n, determine whether it is a palindrome number or not. A number is called a palindrome if it reads the same from forward and backward.

Examples:

Input: n = 12321
Output: True
Explanation: 12321 is a palindrome number because it reads same forward and backward.

Input: n = -121
Output: True
Explanation: We number is palindrome, we mainly ignore sign.

Input: n = 1234
Output: False
Explanation: 1234 is not a palindrome number because it does not read the same forward and backward.

[Expected Approach] - By Reversing The Number

The idea is to find the reverse of the original number and then compare the reversed number with the original number. If the reversed number is same as the original number, the number is palindrome. Otherwise, the number is not a palindrome.


Output
True

Time Complexity : O(d) , where d = log₁₀(n)
Auxiliary space: O(1)

[Alternate Approach] - Using Number as String

When the input number exceeds 1018 , reversing it as an integer can cause overflow in languages like C, C++, and Java. To avoid this, treat the number as a string and compare characters from both ends towards the center. If any pair of characters doesn’t match, it is not a palindrome.


Output
True

Time Complexity : O(d) , where d = log₁₀(n)
Auxiliary space: O(d) , where d = log₁₀(n)

Comment