To check a number is palindrome or not without using any extra space - GeeksforGeeks

06 May, 2021
Given a number ‘n’ and our goal is to find out it is palindrome or not without using any extra space. We can’t make a new copy of number .Examples:  
Input  : 2332
Output : Yes it is Palindrome.
Explanation:
original number = 2332
reversed number = 2332
Both are same hence the number is palindrome.

Input :1111
Output :Yes it is Palindrome.

Input : 1234
Output : No not Palindrome.
 
A recursive solution is discussed in below post. Check if a number is PalindromeIn this post a different solution is discussed. 1) We can compare the first digit and the last digit, then we repeat the process. 2) For the first digit, we need the order of the number. Say, 12321. Dividing this by 10000 would get us the leading 1. The trailing 1 can be retrieved by taking the mod with 10. 3 ) Now, to reduce this to 232. 
(12321 % 10000)/10 = (2321)/10 = 232 
4 ) And now, the 10000 would need to be reduced by a factor of 100. Here is the implementation of the above algorithm :  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find number is palindrome// or not without using any extra space#include <bits/stdc++.h>using namespace std;bool isPalindrome(int); bool isPalindrome(int n){      // Find the appropriate divisor    // to extract the leading digit    int divisor = 1;    while (n / divisor >= 10)        divisor *= 10;     while (n != 0)    {        int leading = n / divisor;        int trailing = n % 10;         // If first and last digit        // not same return false        if (leading != trailing)             return false;         // Removing the leading and trailing        // digit from number        n = (n % divisor) / 10;         // Reducing divisor by a factor        // of 2 as 2 digits are dropped        divisor = divisor / 100;    }    return true;} // Driver codeint main(){    isPalindrome(1001) ? cout << "Yes, it is Palindrome" :    cout << "No, not Palindrome";    return 0;}
// Java program to find number is palindrome// or not without using any extra spacepublic class GFG{         static boolean isPalindrome(int n)    {          // Find the appropriate divisor        // to extract the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;              while (n != 0)        {            int leading = n / divisor;            int trailing = n % 10;                  // If first and last digit            // not same return false            if (leading != trailing)                 return false;                  // Removing the leading and trailing            // digit from number            n = (n % divisor) / 10;                  // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }          // Driver code    public static void main(String args[])    {        if(isPalindrome(1001))            System.out.println("Yes, it is Palindrome");        else            System.out.println("No, not Palindrome");    }}// This code is contributed by Sumit Ghosh
# Python program to find number# is palindrome or not without# using any extra space # Function to check if given number# is palindrome or not without# using the extra spacedef isPalindrome(n):     # Find the appropriate divisor    # to extract the leading digit    divisor = 1    while (n / divisor >= 10):        divisor *= 10     while (n != 0):                 leading = n // divisor        trailing = n % 10                 # If first and last digit        # not same return false        if (leading != trailing):            return False                 # Removing the leading and        # trailing digit from number        n = (n % divisor)//10                 # Reducing divisor by a factor        # of 2 as 2 digits are dropped        divisor = divisor/100             return True # Driver codeif(isPalindrome(1001)):    print('Yes, it is palindrome')else:    print('No, not palindrome') # This code is contributed by Danish Raza
// C# program to find number// is palindrome or not without// using any extra spaceusing System; class GFG{    static bool isPalindrome(int n)    {        // Find the appropriate        // divisor to extract        // the leading digit        int divisor = 1;        while (n / divisor >= 10)            divisor *= 10;             while (n != 0)        {            int leading = n / divisor;            int trailing = n % 10;                 // If first and last digit            // not same return false            if (leading != trailing)                return false;                 // Removing the leading and            // trailing digit from number            n = (n % divisor) / 10;                 // Reducing divisor by            // a factor of 2 as 2            // digits are dropped            divisor = divisor / 100;        }        return true;    }         // Driver code    static public void Main ()    {        if(isPalindrome(1001))            Console.WriteLine("Yes, it " +                         "is Palindrome");        else            Console.WriteLine("No, not " +                            "Palindrome");    }} // This code is contributed by m_kit
<?php// PHP program to find// number is palindrome// or not without using// any extra space function isPalindrome($n){    // Find the appropriate divisor    // to extract the leading digit    $divisor = 1;    while ($n / $divisor >= 10)        $divisor *= 10;     while ($n != 0)    {        $leading = floor($n / $divisor);        $trailing = $n % 10;         // If first and last digit        // not same return false        if ($leading != $trailing)            return false;         // Removing the leading and        // trailing digit from number        $n = ($n % $divisor) / 10;         // Reducing divisor by a        // factor of 2 as 2 digits        // are dropped        $divisor = $divisor / 100;    }    return true;} // Driver codeif(isPalindrome(1001) == true)echo "Yes, it is Palindrome" ;else echo "No, not Palindrome"; // This code is contributed by ajit?>
<script>// javascript program to find number is palindrome// or not without using any extra space     function isPalindrome(n) {        // Find the appropriate divisor        // to extract the leading digit        var divisor = 1;        while (parseInt(n / divisor) >= 10)            divisor *= 10;         while (n != 0) {            var leading = parseInt(n / divisor);            var trailing = n % 10;             // If first and last digit            // not same return false            if (leading != trailing)                return false;             // Removing the leading and trailing            // digit from number            n = parseInt((n % divisor) / 10);             // Reducing divisor by a factor            // of 2 as 2 digits are dropped            divisor = divisor / 100;        }        return true;    }     // Driver code             if (isPalindrome(1001))            document.write("Yes, it is Palindrome");        else            document.write("No, not Palindrome"); // This code is contributed by todaysgaurav</script>
Output:  
Yes, it is Palindrome
This article is contributed by Abhijit Shankhdhar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
todaysgaurav
number-digits
palindrome
Mathematical
Mathematical
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Sieve of Eratosthenes
Program to find GCD or HCF of two numbers
Program for factorial of a number
Print all possible combinations of r elements in a given array of size n
Program for Decimal to Binary Conversion
The Knight's tour problem | Backtracking-1
Operators in C / C++