Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 25 May, 2021

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: 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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<bits/stdc++.h>
#include<string>
#include<iostream>
#include<sstream>
 
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

Java




// Java implementation of the approach
import java.util.*;
 
class GFG{
 
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    String y = String.valueOf(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.charAt(i) == 0)
          continue;
       num += y.charAt(i);
    }
         
    // Return the integer after
    // removing 0s
    return Integer.parseInt(num);
}
 
 
// Function that returns true if
// the given condition is satisfied
public static boolean 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
public static void main(String[] args)
{
    int a = 101;
    int b = 102;
     
    if(check(a, b))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by grandmaster

Python3




# 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")

C#




// C# implementation of the approach
using System;
  
class GFG{
  
// Function to remove zeroes from a number    
public static int remove(int x)
{
     
    // Converting x into a string
    string y = x.ToString();
      
    // 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 Int32.Parse(num);
}
  
// Function that returns true if
// the given condition is satisfied
public static 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
public static void Main(string[] args)
{
    int a = 101;
    int b = 102;
      
    if (check(a, b))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by rutvik_56

PHP




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

Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to remove zeroes from a number
function remove(x)
{
    // Converting x into a string
     
    var y = x.toString();
     
    // To store the new integer without 0s
    var num = "";
    var 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 parseInt(num);
}
 
// Function that returns true if
// the given condition is satisfied
function check(a, b)
{
    // Calculate c
    var 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
var a = 101;
var b = 102;
 
if(check(a, b))
    document.write( "Yes");
else
    document.write( "No");
 
</script>
Output: 
Yes

 

Alternate implementation in Python:

Python3




# Python3 implementation of the approach
  
# Calculate the sum with Zero's intact
d=a+b
  
# Then convert the numbers to string
# replace '0' with '' (blank)
  
a=str(a).replace('0','')
b=str(b).replace('0','')
  
# Now convert the strings back to numbers
# Add the numbers
c=int(a)+int(b)
  
if c==d:
    print('Yes')
else:
    print('No')
  
# This code is contributed by Sandeep Midde



My Personal Notes arrow_drop_up
Recommended Articles
Page :