Compruebe si a + b = c o no después de eliminar todos los ceros de a, b y c

Dados dos enteros a y b , la tarea es sumarlos para obtener c . Después de eso, elimine los ceros de a , b y c y verifique los valores modificados si a + b = c , luego devuelva «SÍ» , de lo contrario, devuelva «NO» .
Ejemplos: 
 

Entrada: a = 101, b = 102 
Salida: SÍ 
101 + 102 = 203. 
Después de eliminar todos los ceros de a, b y c, a = 11, b = 12 y c = 23 
Ahora verifique si a + b = c, es decir, 11 + 12 = 23 . Así que imprime Sí.
Entrada: a = 105, b = 108 
Salida: NO 
Después de eliminar todos los ceros a + b != c , por lo tanto, la salida es NO. 
 

Acercarse: 
 

  • Cree una función para eliminar cero del número n .
  • Compruebe si (eliminar cero (a) + eliminar cero (b) == eliminar cero (a + b)) y luego imprimir , de lo contrario, imprimir NO

A continuación se muestra la implementación del enfoque anterior. 
 

C++

// C++ program to check the sum after
// Removing all zeroes is true or not
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove zeroes
int removeZero(int n)
{
    // Initialize result to zero holds the
    // Result after removing zeroes from no
    int res = 0;
 
    // Initialize variable d to 1 that holds
    // digits of no
    int d = 1;
 
    // Loop while n is greater than zero
    while (n > 0) {
 
        // Check if n mod 10 is not equal to
        // zero
        if (n % 10 != 0) {
 
            // store the result by removing zeroes
            // And increment d by 10
            res += (n % 10) * d;
            d *= 10;
        }
 
        // Go to the next digit
        n /= 10;
    }
 
    // Return the result
    return res;
}
 
// Function to check if sum is true after
// Removing all zeroes.
bool isEqual(int a, int b)
{
    // Call removeZero() for both sides
    // and check whether they are equal
    // After removing zeroes.
 
    if (removeZero(a) + removeZero(b) == removeZero(a + b))
        return true;
 
    return false;
}
 
// Driver code
int main()
{
    int a = 105, b = 106;
    isEqual(a, b) ? cout << "Yes"
                  : cout << "No";
 
    return 0;
}

Java

// Java program to check the sum after
// Removing all zeroes is true or not
public class GfG {
     
    // Function to remove zeroes
    public static int removeZero(int n)
    {
        // Initialize result to zero holds the
        // Result after removing zeroes from no
        int res = 0;
       
        // Initialize variable d to 1 that holds
        // digits of no
        int d = 1;
       
        // Loop while n is greater than zero
        while (n > 0) {
       
            // Check if n mod 10 is not equal to
            // zero
            if (n % 10 != 0) {
       
                // store the result by removing zeroes
                // And increment d by 10
                res += (n % 10) * d;
                d *= 10;
            }
       
            // Go to the next digit
            n /= 10;
        }
       
        // Return the result
        return res;
    }
     
    // Function to check if sum is true after
    // Removing all zeroes.
    public static boolean isEqual(int a, int b)
    {
        // Call removeZero() for both sides
        // and check whether they are equal
        // After removing zeroes.
       
        if (removeZero(a) + removeZero(b) == removeZero(a + b))
            return true;
       
        return false;
    }
     
     public static void main(String []args){
         
        int a = 105, b = 106;
         
        if (isEqual(a, b) == true)
            System.out.println("Yes");
        else
            System.out.println("No");
     }
}
   
// This code is contributed by Rituraj Jain

Python3

# Python 3 program to check the sum after
# Removing all zeroes is true or not
 
# Function to remove zeroes
def removeZero(n):
     
    # Initialize result to zero holds the
    # Result after removing zeroes from no
    res = 0
 
    # Initialize variable d to 1 that
    # holds digits of no
    d = 1
 
    # Loop while n is greater than zero
    while (n > 0):
         
        # Check if n mod 10 is not equal
        # to zero
        if (n % 10 != 0):
             
            # store the result by removing
            # zeroes And increment d by 10
            res += (n % 10) * d
            d *= 10
 
        # Go to the next digit
        n //= 10
 
    # Return the result
    return res
 
# Function to check if sum is true
# after Removing all zeroes.
def isEqual(a, b):
     
    # Call removeZero() for both sides
    # and check whether they are equal
    # After removing zeroes.
    if (removeZero(a) +
        removeZero(b) == removeZero(a + b)):
        return True
 
    return False
 
# Driver code
a = 105
b = 106
if(isEqual(a, b)):
    print("Yes")
else:
    print("No")
 
# This code is contributed
# by sahishelangia

C#

// C# program to check the sum after
// Removing all zeroes is true or not
using System;
class GFG
{
 
// Function to remove zeroes
public static int removeZero(int n)
{
    // Initialize result to zero holds the
    // Result after removing zeroes from no
    int res = 0;
 
    // Initialize variable d to 1 that holds
    // digits of no
    int d = 1;
 
    // Loop while n is greater than zero
    while (n > 0)
    {
 
        // Check if n mod 10 is not equal to
        // zero
        if (n % 10 != 0)
        {
 
            // store the result by removing zeroes
            // And increment d by 10
            res += (n % 10) * d;
            d *= 10;
        }
 
        // Go to the next digit
        n /= 10;
    }
 
    // Return the result
    return res;
}
 
// Function to check if sum is true after
// Removing all zeroes.
public static bool isEqual(int a, int b)
{
    // Call removeZero() for both sides
    // and check whether they are equal
    // After removing zeroes.
 
    if (removeZero(a) +
        removeZero(b) == removeZero(a + b))
        return true;
 
    return false;
}
 
// Driver Code
public static void Main()
{
    int a = 105, b = 106;
     
    if (isEqual(a, b) == true)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP program to check the sum after
// Removing all zeroes is true or not.
 
// Function to remove zeroes
function removeZero($n)
{
    // Initialize result to zero holds the
    // Result after removing zeroes from no
    $res = 0;
 
    // Initialize variable d to 1 that
    // holds digits of no
    $d = 1;
 
    // Loop while n is greater than zero
    while ($n > 0)
    {
 
        // Check if n mod 10 is not equal
        // to zero
        if ($n % 10 != 0)
        {
 
            // store the result by removing
            // zeroes and increment d by 10
            $res += ($n % 10) * $d;
            $d *= 10;
        }
 
        // Go to the next digit
        $n = floor($n / 10);
    }
 
    // Return the result
    return $res;
}
 
// Function to check if sum is true
// after Removing all zeroes.
function isEqual($a, $b)
{
    // Call removeZero() for both sides
    // and check whether they are equal
    // After removing zeroes.
    if (removeZero($a) +
        removeZero($b) == removeZero($a + $b))
        return true;
 
    return false;
}
 
// Driver code
$a = 105 ;
$b = 106 ;
 
if (isEqual($a, $b))
    echo "Yes";
else
    echo "No";
 
// This code is contributed by Ryuga
?>

Javascript

<script>
// Javascript program to check the sum after
// Removing all zeroes is true or not
 
// Function to remove zeroes
function removeZero( n)
{
    // Initialize result to zero holds the
    // Result after removing zeroes from no
    let res = 0;
 
    // Initialize variable d to 1 that holds
    // digits of no
    let d = 1;
 
    // Loop while n is greater than zero
    while (n > 0) {
 
        // Check if n mod 10 is not equal to
        // zero
        if (n % 10 != 0) {
 
            // store the result by removing zeroes
            // And increment d by 10
            res += (n % 10) * d;
            d *= 10;
        }
 
        // Go to the next digit
        n = Math.floor(n/10);
    }
 
    // Return the result
    return res;
}
 
// Function to check if sum is true after
// Removing all zeroes.
function isEqual( a, b)
{
    // Call removeZero() for both sides
    // and check whether they are equal
    // After removing zeroes.
 
    if (removeZero(a) + removeZero(b) == removeZero(a + b))
        return true;
 
    return false;
}
 
 
 
    // Driver Code
     
    let a = 105, b = 106;
    if (isEqual(a, b) == true)
        document.write("Yes");
    else
        document.write("No");
     
</script>
Producción: 

No

 

Complejidad de tiempo: O (log 10 n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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