Chemical Equation Balancer

Source Code, CLI Version

This is a java program for CLI. The source is here, chembal.tar. If you want just a .jar file to run, you can use chembal.jar.

Originally this software was designed to run in an ancient browser technology known as a "Java applet". If you've never heard of this, you're fortunate. The CLI version strips out all this functionality.


To balance a reaction simply list all the reactants and products. Seperate different compounds with + signs. To add a charge, simply follow the chemical by a caret (^) and then the charge. If the charge is positive you should leave off the + sign before the charge, but if it's negative it should be prefixed by a - sign.


  • Cu + HNO3 + Cu(NO3)2 +NO+H2O
  • H2O + H^ + Cl^- + MnO4^- + Cl2 + Mn^2
  • H2 + O2 + H2O
  • Na2CO3 + HCl --> NaCl + CO2 + H2O
  • O + O2 --> O3
  • Na2CO3 + HCl + FeCl3 + Fe2O3 + NaCl + H2O + CO2

More than one solution?

You can get multiple solutions to a reaction if there is more than one way to balance it (where taking multiples of all the coefficients does not count as a separate way). In the language of linear algebra, the applet outputs a "basis" for all the solutions.

The arrow is in the wrong place!

The program automatically tries to put the arrow in a sensible place so that the reaction balances properly. It can do things that make no sense if there are multiple solutions. You will need to take linear combinations of the solutions yourself to get the arrow in the right place.

Less Technical Example of how the applet works:

Consider this example: H2 + O2 --> H2O. Using algebra, we will make a system of equations to solve the chemical equation. Let's force the coefficient of H2 to be a, the coefficient of O2 to be b and the coefficient of H2O to be c. Then since there are the same number of hydrogen atoms on each side of the chemical equation, we know that a = c. Since there are the same number of oxygen atoms on each side, b = 2c. Notice that a = 2, b=1 and c = 2 is a solution to this system. It is not the only one, but it is the "smallest" one that uses positive integers. Therefore the balanced reaction is 2H2 + O2 --> 2H2O. The applet above does this same process, except on a larger scale, using the tools of linear algebra.

More Technical Explaination

List the compounds in the chemical reaction, and call them c1, c2, ... , cn. Let the integer ai denote the number of occurences of ci on the left hand side of the balanced equation (or equivalently, the negation of the number of occurences on the right hand side of the balanced equation). Suppose there are m different types of atoms involved in the reaction. Let bi,j denote the occurences of atom type j in ci. Then for a fixed j there must be the same number of this type of atom on either side of the equation; so it follows that b1,ja1 + b2,ja2 + ... + bn,jan = 0. Therefore there are m equations constraining the values of the ai.

These equations may be put into matrix form, Bx = 0. In simple terms, the applet solves this system of equations and returns the answer. However, it's not really that simple. Obviously, x = 0 is a solution to the above matrix equation. But if y ≠ 0 and By = 0 then B(my) = 0 for any scalar m. This means the system has one solutions, or infinitely many. If there is only one solution, the chemical equation does not balance. If all the solutions to the equations are scalar multiples of each other, then choosing any one is fine, since one may multiply both sides of a chemical equation by a scalar. It gets trickier when the solutions are not scalar multiples of each other. In linear algebraic terms, the applet finds a basis for the vector space of solutions to the matrix equation and outputs each basis vector as a chemical equation. Thus, when you get an output with multiple answers, this means you can add and subtract these chemical equations in order to produce any way to balance the equation. This happens when two reactions are happening simultaneously but independently; however there are more complicated situations where this happens as well.