Para verificar que un número es palíndromo o no sin usar ningún espacio adicional

Dado un número ‘n’, nuestro objetivo es averiguar si es palíndromo o no sin usar 
ningún espacio adicional. No podemos hacer una nueva copia del número.
Ejemplos: 
 

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.

Una solución recursiva se discute en la publicación a continuación. 
Comprobar si un número es palíndromo
En esta publicación se analiza una solución diferente. 
1) Podemos comparar el primer dígito y el último dígito, luego repetimos el proceso. 
2) Para el primer dígito, necesitamos el orden del número. Digamos, 12321. Dividir esto por 10000 nos daría el 1 principal. El 1 final se puede recuperar tomando el mod con 10. 
3) Ahora, para reducir esto a 232. 

(12321 % 10000)/10 = (2321)/10 = 232 

4) Y ahora, el 10000 tendría que reducirse en un factor de 100. 
Aquí está la implementación del algoritmo anterior: 
 

C++

// 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 code
int main()
{
    isPalindrome(1001) ? cout << "Yes, it is Palindrome" :
    cout << "No, not Palindrome";
    return 0;
}

Java

// Java program to find number is palindrome
// or not without using any extra space
public 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

Python3

# 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 space
def 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 code
if(isPalindrome(1001)):
    print('Yes, it is palindrome')
else:
    print('No, not palindrome')
 
# This code is contributed by Danish Raza

C#

// C# program to find number
// is palindrome or not without
// using any extra space
using 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
// 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 code
if(isPalindrome(1001) == true)
echo "Yes, it is Palindrome" ;
else
 
echo "No, not Palindrome";
 
// This code is contributed by ajit
?>

Javascript

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

Producción: 
 

Yes, it is Palindrome

Este artículo es una contribución de Abhijit Shankhdhar . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *