Program to balance the given Chemical Equation | Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver Code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function balance ( x , y , p , q ) { let b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; let temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } document . write ( b1 + " " + b2 + " " + b3 + " " ) ; } let x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ;