The Wayback Machine - https://web.archive.org/web/20250116183101/https://www.geeksforgeeks.org/check-if-a-number-is-palindrome/
Open In App

Check if a number is Palindrome

Last Updated : 05 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a positive 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: true
Explanation: 12321 is a Palindrome number because after reversing its digits, the number becomes 12321 which is same as the original number.

Input: n = 1234
Output: false
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] Determine Palindrome by Reversing and Comparing

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.

C++
#include <iostream>
using namespace std;

bool checkPalindrome(int n) {
    int reverse = 0;
  
    // Copy of the original number so that the original
    // number remains unchanged while finding the reverse
    int temp = 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 == n);
}

int main() {
    int n = 12321;
    if (checkPalindrome(n) == 1) {
        cout << "true\n";
    }
    else {
        cout << "false\n";
    }
    return 0;
}
Java
class GfG {
    static boolean checkPalindrome(int n) {
        int reverse = 0;
      
        // Copy of the original number so that the original
        // number remains unchanged while finding the reverse
        int temp = 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 == n);
    }

  public static void main(String[] args) {
        int n = 12321;
        if (checkPalindrome(n)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
}
Python
def checkPalindrome(n):
    reverse = 0
    
    # Copy of the original number so that the original
    # number remains unchanged while finding the reverse
    temp = 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 == n

if __name__ == "__main__":
  n = 12321
  if checkPalindrome(n):
    print("true")
  else:
    print("false")
C#
using System;

public class GfG {
    public static bool CheckPalindrome(int n){
        int reverse = 0;
      
        // Copy of the original number so that the original
        // number remains unchanged while finding the reverse
        int temp = 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 == n);
    }

 public static void Main() {
        int n = 12321;
        if (CheckPalindrome(n)) {
            Console.WriteLine("true");
        }
        else{
            Console.WriteLine("false");
        }
    }
}
JavaScript
function checkPalindrome(n) {
    let reverse = 0;
    
    // Copy of the original number so that the original
    // number remains unchanged while finding the reverse
    let temp = 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 === n;
}

// Driver code
    let n = 12321;
    if (checkPalindrome(n)) {
        console.log("true");
    } else {
        console.log("false");
    }

Output
true

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

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

C++14
#include <iostream>
using namespace std;

int checkPalindrome(int n) {
  string s = to_string(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;
    }
  
    // If the above loop doesn't return then it is palindrome
    return true;
}

int main() { 
    int n
        = 11211;
    if (checkPalindrome(n) == true)
        cout << "true";
    else
        cout << "false";
    return 0;
}
Java
import java.io.*;

class GfG {
    static boolean checkPalindrome(int n) {
      
        String s = Integer.toString(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;
        }
      
        // If the above loop doesn't return then it is palindrome
        return true;
    }

    public static void main(String[] args) {
        int n
            = 1221;

        if (checkPalindrome(n) == true)
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
def checkPalindrome(n):
    s = str(n)
    length = len(s)
    
    for i in range(length // 2):
        # Compare characters from the start and end
        if s[i] != s[length - i - 1]:
            return False

    # If no mismatch is found, it's a palindrome
    return True


if __name__ == "__main__":
  n = 1356531
  if(checkPalindrome(n) == True):
      print("true")
  else:
      print("false")
C#
using System;

class GfG {
    static bool checkPalindrome(int n) {
    
          string s = 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;
        }

    // If the above loop doesn't return then it is palindrome
        return true;
    }

  public static void Main() {
        int n=123454321;

        if (checkPalindrome(n) == true)
            Console.Write("true");
        else
            Console.Write("false");
    }
}
JavaScript
function checkPalindrome(n){
    const s = n.toString();
    var len = s.length;
    
    for (var i = 0; i < len / 2; i++) {
       
        // Comparing ith character 
        // from starting and len-ith
        // character from end
        if (s[i] != s[len - i - 1])
            return false;
    }
   
    // If the above loop doesn't return then it is
    // palindrome
    return true;
}
 
// Driver Code
let n = 12321;
if (checkPalindrome(n) == true)
    console.log("true");
else
    console.log("false");

Output
true

Time Complexity: O(|s|) or O(number of digits)
Auxiliary Space: O(1)  


Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.

And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!


Next Article

Similar Reads

three90RightbarBannerImg