Program to balance the given Chemical Equation

Given the values x, y, p, q of a simple chemical equation of the type:
b_{1}\text{ }A_{x} + b_{2}\text{ }B_{y}  \rightarrow  b_{3}\text{ }A_{p}B_{q}
The task is to find the values of constants b1, b2, b3 such that the equation is balanced on both sides and it must be the reduced form.

Examples:

Input: x = 2, y = 3, p = 4, q = 5
Output: b1 = 6, b2 = 5, b3 = 3

Input: x = 1, y = 2, p = 3, q = 1
Output: b1 = 3, b2 = 2, b3 = 1

Approach:



  1. Check if p % x = 0 and q % y = 0 or not.
  2. If yes, then simply we can say that
    b1 = p / x, 
    b2 = q / y, and 
    b3 = 1
  3. Else we need to use gcd to compute b1, b2, b3. We need the reduced form so gcd can help with it.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to balance
// the given Chemical Equation
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate GCD
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to calculate b1, b2 and b3
void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int 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;
  
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
  
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
  
    cout << b1 << " " << b2
         << " " << b3 << endl;
}
  
// Driver code
int main()
{
    int x = 2, y = 3, p = 4, q = 5;
  
    balance(x, y, p, q);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to balance
// the given Chemical Equation
  
import java.util.*;
  
class GFG{
   
// Function to calculate GCD
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
   
// Function to calculate b1, b2 and b3
static void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int 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;
   
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
   
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
   
    System.out.print(b1 + " " +  b2
        + " " +  b3 +"\n");
}
   
// Driver code
public static void main(String[] args)
{
    int x = 2, y = 3, p = 4, q = 5;
   
    balance(x, y, p, q);
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to balance
# the given Chemical Equation
  
# Function to calculate GCD
def gcd(a,b):
    if (b == 0):
        return a
    return gcd(b, a % b)
  
# Function to calculate b1, b2 and b3
def balance(x, y, p, q):
  
    # Variable declaration
    if (p % x == 0 and q % y == 0):
        b1 = p // x
        b2 = q // y
        b3 = 1
  
    else:
        p = p * y
        q = q * x
        b3 = x * y
  
        # temp variable to store gcd
        temp = gcd(p, gcd(q, b3))
  
        # Computing GCD
        b1 = p // temp
        b2 = q // temp
        b3 = b3 // temp
  
    print(b1,b2,b3)
  
# Driver code
if __name__ == '__main__':
    x = 2
    y = 3
    p = 4
    q = 5
  
    balance(x, y, p, q)
  
# This code is contributed by Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to balance
// the given Chemical Equation
  
using System;
  
public class GFG{
  
// Function to calculate GCD
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to calculate b1, b2 and b3
static void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int 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;
  
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
  
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
  
    Console.Write(b1 + " " + b2
                  + " " + b3 +"\n");
}
  
// Driver code
public static void Main(String[] args)
{
    int x = 2, y = 3, p = 4, q = 5;
  
    balance(x, y, p, q);
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

6 5 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.