Check whether a + b = c or not after removing all zeroes from a,b and c
Last Updated :
04 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int removeZero( int n)
{
int res = 0;
int d = 1;
while (n > 0) {
if (n % 10 != 0) {
res += (n % 10) * d;
d *= 10;
}
n /= 10;
}
return res;
}
bool isEqual( int a, int b)
{
if (removeZero(a) + removeZero(b) == removeZero(a + b))
return true ;
return false ;
}
int main()
{
int a = 105, b = 106;
isEqual(a, b) ? cout << "Yes"
: cout << "No" ;
return 0;
}
|
Java
public class GfG {
public static int removeZero( int n)
{
int res = 0 ;
int d = 1 ;
while (n > 0 ) {
if (n % 10 != 0 ) {
res += (n % 10 ) * d;
d *= 10 ;
}
n /= 10 ;
}
return res;
}
public static boolean isEqual( int a, int b)
{
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" );
}
}
|
Python3
def removeZero(n):
res = 0
d = 1
while (n > 0 ):
if (n % 10 ! = 0 ):
res + = (n % 10 ) * d
d * = 10
n / / = 10
return res
def isEqual(a, b):
if (removeZero(a) +
removeZero(b) = = removeZero(a + b)):
return True
return False
a = 105
b = 106
if (isEqual(a, b)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
public static int removeZero( int n)
{
int res = 0;
int d = 1;
while (n > 0)
{
if (n % 10 != 0)
{
res += (n % 10) * d;
d *= 10;
}
n /= 10;
}
return res;
}
public static bool isEqual( int a, int b)
{
if (removeZero(a) +
removeZero(b) == removeZero(a + b))
return true ;
return false ;
}
public static void Main()
{
int a = 105, b = 106;
if (isEqual(a, b) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function removeZero( $n )
{
$res = 0;
$d = 1;
while ( $n > 0)
{
if ( $n % 10 != 0)
{
$res += ( $n % 10) * $d ;
$d *= 10;
}
$n = floor ( $n / 10);
}
return $res ;
}
function isEqual( $a , $b )
{
if (removeZero( $a ) +
removeZero( $b ) == removeZero( $a + $b ))
return true;
return false;
}
$a = 105 ;
$b = 106 ;
if (isEqual( $a , $b ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function removeZero( n)
{
let res = 0;
let d = 1;
while (n > 0) {
if (n % 10 != 0) {
res += (n % 10) * d;
d *= 10;
}
n = Math.floor(n/10);
}
return res;
}
function isEqual( a, b)
{
if (removeZero(a) + removeZero(b) == removeZero(a + b))
return true ;
return false ;
}
let a = 105, b = 106;
if (isEqual(a, b) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...