Comprobar si dos números tienen el mismo número de dígitos

Dados dos números enteros A y B , la tarea es verificar si ambos números tienen el mismo número de dígitos.
Ejemplos: 
 

Entrada: A = 12, B = 1 
Salida: No
Entrada: A = 20, B = 99 
Salida: Sí 
 

Enfoque: Si bien ambos números son > 0, siga dividiendo ambos números por 10. Finalmente, verifique si ambos números son 0. Si alguno de ellos no es 0, entonces tenían un número desigual de dígitos.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that return true if A and B
// have same number of digits
bool sameLength(int A, int B)
{
    while (A > 0 && B > 0) {
        A = A / 10;
        B = B / 10;
    }
 
    // Both must be 0 now if
    // they had same lengths
    if (A == 0 && B == 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int A = 21, B = 1;
 
    if (sameLength(A, B))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function that return true if A and B
// have same number of digits
static boolean sameLength(int A, int B)
{
    while ((A > 0) && (B > 0))
    {
        A = A / 10;
        B = B / 10;
    }
 
    // Both must be 0 now if
    // they had same lengths
    if ((A == 0 )&& (B == 0))
        return true;
    return false;
}
 
// Driver code
public static void main (String[] args)
{
 
    int A = 21, B = 1;
    if (sameLength(A, B))
        System.out.println ("Yes");
    else
        System.out.println("No");
 
}
}
 
// This code is contributed by @tushil.

Python3

     
# Python implementation of the approach
 
# Function that return true if A and B
# have same number of digits
def sameLength(A, B):
    while (A > 0 and B > 0):
        A = A / 10;
        B = B / 10;
 
    # Both must be 0 now if
    # they had same lengths
    if (A == 0 and B == 0):
        return True;
    return False;
 
# Driver code
A = 21; B = 1;
 
if (sameLength(A, B)):
    print("Yes");
else:
    print("No");
 
# This code contributed by PrinciRaj1992

C#

// C# implementation of the approach
using System;
 
class GFG
{
         
// Function that return true if A and B
// have same number of digits
static bool sameLength(int A, int B)
{
    while ((A > 0) && (B > 0))
    {
        A = A / 10;
        B = B / 10;
    }
 
    // Both must be 0 now if
    // they had same lengths
    if ((A == 0 )&& (B == 0))
        return true;
    return false;
}
 
// Driver code
static public void Main ()
{
         
    int A = 21, B = 1;
    if (sameLength(A, B))
            Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
 
}
}
 
// This code is contributed by ajit..

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function that return true if A and B
// have same number of digits
function sameLength(A, B)
{
    while (A > 0 && B > 0) {
        A = parseInt(A / 10);
        B = parseInt(B / 10);
    }
 
    // Both must be 0 now if
    // they had same lengths
    if (A == 0 && B == 0)
        return true;
    return false;
}
 
// Driver code
    let A = 21, B = 1;
 
    if (sameLength(A, B))
        document.write("Yes");
    else
        document.write("No");
 
</script>
Producción: 

No

 

Complejidad del tiempo: O(log 10 (min(A, B)))

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por prajmsidc 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 *