Check if a + b = c is valid after removing all zeroes from a, b and c

Given two integers a and b. Now, c can be found as a + b = c. The task is to check if the equation is still valid after removing all zeroes from a, b and c. If valid then print Yes else print No.

Examples:

Input: a = 101, b = 102
Output: Yes
Current equation is 101 + 102 = 203
After removing 0s, 11 + 12 = 23 (which is still correct)



Input: a = 105, b = 106
Output: No
105 + 106 = 211
15 + 16 = 211 (Incorrect)

Approach:

  • Calculate c.
  • Remove all 0s from a, b and c.
  • Check if the new values form a correct equation.

Below is the implementation of the above approach:

C++

// C++ implementation of the approach
#include
#include
#include
#include

using namespace std;

// Function to remove zeroes from a number
int remove(int x)
{
// Converting x into a string

string y = to_string(x);

// To store the new integer without 0s
string num;
int i;
for(i = 0; i < y.length(); i++) { // Skip if current character is 0 if(y[i] == 0) continue; num += y[i]; } // Return the integer after removing 0s return stoi(num); } // Function that returns true if // the given condition is satisfied bool check(int a, int b) { // Calculate c int c = a + b; // Remove 0s from a, b and c a = remove(a); b = remove(b); c = remove(c); // Check if the equation is still correct if((a + b) == c) return true; else return false; } // Driver code int main() { int a = 101; int b = 102; if(check(a, b)) cout << "Yes"; else cout << "No"; } // This code is contributed by ita_c [tabby title = "Python3"]

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function that returns true if 
# the given condition is satisfied
def check(a, b):
      
    # Calculate c
    c = a + b
      
    # Remove 0s from a, b and c
    a = remove(a)
    b = remove(b)
    c = remove(c)
      
    # Check if the equation is still correct
    if((a + b) == c):
        return True
    else:
        return False
  
# Function to remove zeroes from a number
def remove(x):
      
    # Converting x into a string
    y = str(x)
      
    # To store the new integer without 0s
    num = ""
    for i in range(len(y)):
          
        # Skip if current character is 0
        if(y[i] == "0"):
            continue
        num += y[i]
          
    # Return the integer after removing 0s
    return int(num)
  
# Driver code
a = 101
b = 102
  
if(check(a, b)):
    print("Yes")
else:
    print("No")

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function that returns true if
// the given condition is satisfied 
function check($a, $b)
{
      
    // Calculate c 
    $c = $a + $b ;
      
    // Remove 0s from a, b and c 
    $a = remove($a);
    $b = remove($b);
    $c = remove($c);
      
    // Check if the equation is 
    // still correct 
    if(($a + $b) == $c
        return true;
    else
        return false;
}
  
// Function to remove zeroes 
// from a number 
function remove($x)
      
    // Converting x into a string 
    $y = (string)$x;
      
    // To store the new integer without 0s 
    $num = "";
    for ($i = 0; $i < strlen($y); $i++)
    {
          
        // Skip if current character is 0 
        if($y[$i] == "0"
            continue ;
        $num .= $y[$i];
    }
          
    // Return the integer after removing 0s 
    return (int)$num;
}
  
// Driver code 
$a = 101;
$b = 102;
  
if(check($a, $b))
    echo "Yes"
else
    echo "No"
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

Yes


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.



Improved By : Ryuga, Ita_c



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.