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++
// 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> |
No
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!