GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • DSA Tutorial
  • Interview Questions
  • Quizzes
  • Must Do
  • Advanced DSA
  • System Design
  • Aptitude
  • Puzzles
  • Interview Corner
  • DSA Python
  • reverse of the original number
  • Logic Building Problems2 min read
  • Analysis of Algorithms1 min read
  • Array3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Check if a number is Palindrome - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • DSA Tutorial
  • Interview Questions
  • Quizzes
  • Must Do
  • Advanced DSA
  • System Design
  • Aptitude
  • Puzzles
  • Interview Corner
  • DSA Python

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.

Try It Yourself
redirect icon

Table of Content

  • [Expected Approach] Reversing The Number
  • [Alternate Approach] Number as String

[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");
}

Output
true

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");
}

Output
true

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

Comment

Explore

    DSA Fundamentals
    • Logic Building Problems2 min read
    • Analysis of Algorithms1 min read
    Data Structures
    • Array3 min read
    • String2 min read
    • Hashing2 min read
    • Linked List3 min read
    • Stack2 min read
    • Queue1 min read
    • Tree2 min read
    • Graph3 min read
    Algorithms
    • Searching Algorithms2 min read
    • Sorting Algorithms3 min read
    • Introduction to Recursion15 min read
    • Greedy Algorithms3 min read
    • Graph Algorithms3 min read
    • Dynamic Programming2 min read
    • Bitwise Algorithms4 min read
    Advanced
    • Segment Tree2 min read
    • Binary Indexed Tree12 min read
    • Trie Data Structure15+ min read
    • Square Root (Sqrt) Decomposition Algorithm15+ min read
    Interview Preparation
    • Software Developer Interview Preparation2 min read
    • GFG 1602 min read
    • Coding Practice1 min read
    • POTD2 min read
    Courses
    • Placement 360 Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402877 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.