How do I check if a number is a palindrome?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Overview
A palindrome is a number (or text) that reads the same forward and backward. Checking if a number is a palindrome is a common problem encountered in computer science and mathematics. This article provides a detailed guide on various approaches to determine if a number is a palindrome, complete with technical explanations and examples.
Palindrome Checking Approaches
1. String Reversal Method
The most straightforward approach to check if a number is a palindrome is by converting it to a string, reversing the string, and comparing it to the original string.
Steps:
- Convert the number to a string.
- Reverse the string.
- Compare the reversed string with the original string.
Example:
2. Numerical Reversal Method
This method involves reversing the digits of the number itself without converting it to a string. It's often used to avoid extra space usage or where string operations are not preferred.
Steps:
- Initialize a variable to store the reversed number.
- Extract digits from the number in reverse order.
- Build the reversed number.
- Compare the original number with the reversed number.
Mathematical Insights:
- Extract each digit using modulo and integer division.
- Shift digits in the reversed number using multiplication and addition.
Example:
3. Half Reversal Method
Instead of reversing the entire number, the half-reversal method involves reversing only the second half of the digits. This method is more efficient for large numbers.
Steps:
- Reverse only the second half of the number's digits.
- Compare the reversed half with the first half.
Example:
Key Points Summary
Below is a table summarizing the key characteristics of the palindrome checking methods:
| Method | Approach | Space Complexity | Time Complexity |
| String Reversal | Convert to string & reverse | ||
| Numerical Reversal | Reverse using digits & compare | ||
| Half Reversal | Reverse half & compare |
Additional Details
Palindromes in Different Bases
A number's palindromic property can change depending on the base. For instance, 121 in base 10 is a palindrome, but it is not in base 2 (binary). Typically, a generalized approach might involve converting the number into the appropriate base and checking for palindrome properties accordingly.
Applications of Palindromes
- Computer Science: String algorithms use palindrome properties in problems like longest palindromic substring, palindrome partitioning, etc.
- Cryptography: Certain encryption schemes may employ palindromes due to their reversible nature.
- Mathematics: Palindromic numbers are part of sequences and patterns that bear significance in number theory.
In summary, checking if a number is a palindrome can be achieved through various methods, each suitable for different scenarios. Understanding these methods provides fundamental insight into number theory and algorithm design.
Related reading
- How Do I Choose Between a Hash Table and a Trie Prefix Tree?
- how do I create a line of arbitrary thickness using Bresenham?
- How do I create a random path?
- How do I determine the longest similar portion of several strings?
- How do I determine whether my calculation of pi is accurate?
- How do I efficiently determine if a polygon is convex, non-convex or complex?
- How do I draw an ellipse with arbitrary orientation pixel by pixel?
- How do I dynamically assign properties to an object in TypeScript?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.