Open In App

Check if a number is Palindrome

Last Updated : 15 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer n, find whether the number is Palindrome or not. A number is a Palindrome if it remains the same when its digits are reversed.

Examples:

Input: n = 12321
Output: Yes
Explanation: 12321 is a Palindrome number because after reversing its digits, the number becomes 12321 which is the same as the original number.

Input: n = 1234
Output: No
Explanation: 1234 is not a Palindrome number because after reversing its digits, the number becomes 4321 which is different from the original number.

[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.

Below is given the implementation:

C++
// C++ program to check if the given 
// number is a palindrome
#include <iostream>
using namespace std;

// Function to check if the number is palindrome
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 << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}
Java
// Java program to check if the given 
// number is a palindrome
class GFG {

    // Function to check if the number is palindrome
    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("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if the given 
# number is a palindrome

# Function to check if the number is palindrome
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))

n = 12321
if isPalindrome(n) == True:
    print("Yes")
else:
    print("No")
C#
// C# program to check if the given 
// number is a palindrome
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("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to check if the given 
// number is a palindrome

// Function to check if the number is palindrome
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));
}

let n = 12321;
if (isPalindrome(n) === true) {
    console.log("Yes");
}
else {
    console.log("No");
}

Output
true

Time Complexity : O(log10(n)) = O(number of digits)
Auxiliary space : O(1)

[Alternate Approach] – Using Number as String

When the input number exceeds 1018, then finding the reverse can cause overflow in languages like: C, C++, Java, etc. So, we can take the input number as a string and run a loop from starting to length/2 and compare the first character (numeric) with the last character of the string, second character to second last one, and so on. If any character mismatches, the string wouldn’t be a palindrome.

Below is given the implementation:

C++
// C++ program to check if the given 
// number is a palindrome
#include <iostream>
using namespace std;

// Function to check if the number is palindrome
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 << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}
Java
// Java program to check if the given 
// number is a palindrome
class GFG {

    // Function to check if the number is palindrome
    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("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
Python
# Python program to check if the given 
# number is a palindrome

# Function to check if the number is palindrome
def isPalindrome(n):

    # Convert the absolute value
    # of number to string
    s = str(abs(n))
    length = len(s)

    for i in range(length // 2):

        # Comparing i th character from starting
        #  and len-i th character from end
        if s[i] != s[length - i - 1]:
            return False
    return True

n = 12321
if isPalindrome(n) == True:
    print("Yes")
else:
    print("No")
C#
// C# program to check if the given 
// number is a palindrome
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("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to check if the given 
// number is a palindrome

// Function to check if the number is palindrome
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;
}

let n = 12321;
if (isPalindrome(n) === true) {
    console.log("Yes");
}
else {
    console.log("No");
}

Output
Yes

Time Complexity : O(log10(n)) = O(number of digits)
Space Complexity : O(log10(n)) = O(number of digits), to store the number as string.



Next Article

Similar Reads