Check whether a + b = c or not after removing all zeroes from a,b and c

Given two integers a and b, the task is to add them to get c. After that remove zeroes from a, b and c and check for modified values if a + b = c then return “YES” else return “NO”.

Examples:

Input: a = 101, b = 102
Output: YES
101 + 102 = 203.
After removing all zeroes from a, b and c, a = 11, b = 12 and c = 23
Now check if a + b = c i.e. 11 + 12 = 23 . So print Yes.



Input: a = 105, b = 108
Output: NO
After removing all zeroes a + b!= c, therefore the output is NO.

Approach:

  • Create a function to removeZero from the number n.
  • Check if(removeZero(a)+removeZero(b)==removeZero(a+b)) then print YES else print NO

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 then 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 then 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 *= 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

No


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.