GeezerVison is showing www.geeksforgeeks.org — Check if a number is Palindrome - GeeksforGeeks
Check if a number is Palindrome
Last Updated : 4 Nov, 2026
Given an integer n. The task is to find if it is a palindrome.
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] Reversing The Number
- Find the reverse of the original number
- Compare the reversed number with the original number. If the reversed is same as the original the number is palindrome, otherwise not.
C++
#include <iostream>
using namespace std;
bool isPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
int temp = abs(n);
while (temp != 0)
{
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number,
// the number is palindrome
return (reverse == abs(n));
}
int main()
{
int n = 12321;
if (isPalindrome(n) == 1)
{
cout << "true";
}
else
{
cout << "false";
}
return 0;
}
Java
class GFG {
static boolean isPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the
// reverse
int temp = Math.abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse == Math.abs(n));
}
public static void main(String[] args)
{
int n = 12321;
if (isPalindrome(n) == true) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
Python
def isPalindrome(n):
reverse = 0
# Copy of the original number so that the original
# number remains unchanged while finding the reverse
temp = abs(n)
while temp != 0:
reverse = (reverse * 10) + (temp % 10)
temp = temp // 10
# If reverse is equal to the original number, the
# number is palindrome
return (reverse == abs(n))
if __name__ == "__main__":
n = 12321
if isPalindrome(n) == True:
print("True")
else:
print("False")
C#
using System;
class GFG {
// Function to check if the number is palindrome
static bool isPalindrome(int n)
{
int reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the
// reverse
int temp = Math.Abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = temp / 10;
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse == Math.Abs(n));
}
static void Main()
{
int n = 12321;
if (isPalindrome(n) == true) {
Console.WriteLine("true");
}
else {
Console.WriteLine("false");
}
}
}
JavaScript
function isPalindrome(n)
{
let reverse = 0;
// Copy of the original number so that the original
// number remains unchanged while finding the reverse
let temp = Math.abs(n);
while (temp != 0) {
reverse = (reverse * 10) + (temp % 10);
temp = Math.floor(temp / 10);
}
// If reverse is equal to the original number, the
// number is palindrome
return (reverse === Math.abs(n));
}
// Driver Code
let n = 12321;
if (isPalindrome(n) === true) {
console.log("true");
}
else {
console.log("false");
}
Time Complexity : O(d) , where d = log₁₀(n)
Space Complexity: O(1)
[Alternate Approach] 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 overflow,
- Convert the number into a string and compare characters from both ends towards the center.
- If any pair of characters does not match, the number is not a palindrome. Otherwise it is palindrome
C++
#include <iostream>
using namespace std;
bool isPalindrome(int n)
{
// Convert the absolute value
// of number to string
string s = to_string(abs(n));
int len = s.length();
for (int i = 0; i < len / 2; i++)
{
// Comparing i th character from starting
// and len-i th character from end
if (s[i] != s[len - i - 1])
return false;
}
return true;
}
int main()
{
int n = 12321;
if (isPalindrome(n) == 1)
{
cout << "true";
}
else
{
cout << "false";
}
return 0;
}
Java
class GFG {
static boolean isPalindrome(int n)
{
// Convert the absolute value
// of number to string
String s = Integer.toString(Math.abs(n));
int len = s.length();
for (int i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s.charAt(i) != s.charAt(len - i - 1))
return false;
}
return true;
}
public static void main(String[] args)
{
int n = 12321;
if (isPalindrome(n) == true) {
System.out.println("true");
}
else {
System.out.println("false");
}
}
}
Python
def isPalindrome(n):
# Convert the absolute value
# of number to string
s = str(abs(n))
length = len(s)
for i in range(length // 2):
# Compare characters from both ends
if s[i] != s[length - i - 1]:
return False
return True
if __name__ == "__main__":
n = 12321
if isPalindrome(n) == True:
print("True")
else:
print("False")
C#
using System;
class GFG {
// Function to check if the number is palindrome
static bool isPalindrome(int n)
{
// Convert the absolute value
// of number to string
string s = Math.Abs(n).ToString();
int len = s.Length;
for (int i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s[i] != s[len - i - 1])
return false;
}
return true;
}
static void Main()
{
int n = 12321;
if (isPalindrome(n) == true) {
Console.WriteLine("true");
}
else {
Console.WriteLine("false");
}
}
}
JavaScript
function isPalindrome(n)
{
// Convert the absolute value
// of number to string
let s = Math.abs(n).toString();
let len = s.length;
for (let i = 0; i < len / 2; i++) {
// Comparing i th character from starting
// and len-i th character from end
if (s[i] !== s[len - i - 1])
return false;
}
return true;
}
// Driver Code
let n = 12321;
if (isPalindrome(n) === true) {
console.log("true");
}
else {
console.log("false");
}
Time Complexity: O(d) , where d = log₁₀(n)
Space Complexity: O(d) , where d = log₁₀(n)