Back to Home

Chemical Equation Balancer

Random Balance
Balanced Equation:

Syntax Guide

Feature & demoInputEquation
SubscriptN = N2N → N2
CompoundH2 + O2 = H2OH2 + O2 → H2O
GroupMg(OH)2 = MgO + H2OMg(OH)2 → MgO + H2O
IonH^+ + CO3^2- = H2O + CO2H+ + CO32− → H2O + CO2
ElectronFe^3+ + e = FeFe3+ + e → Fe
No spaceA3^-+B2^2+=A5B+eA3 + B22+ → A5B + e
More spaceC 3 H 5 ( O H ) 3 + O 2 = H 2 O + C O 2C3H5(OH)3 + O2 → H2O + CO2
Optional 1H1^1+ + e = H1^1-H+ + e → H
Flexible nameFoo^5+ + Bar^3- = FooBar2 + FooBar^-Foo5+ + Bar3− → FooBar2 + FooBar

Error Message Guide

Syntax error

Your input does not describe a proper chemical equation. Check each letter carefully, and follow the examples as a guide to the correct syntax.

All-zero solution

The only mathematical solution to your equation has all coefficients set to zero, which is a trivial solution for every chemical equation. For example, C → N2 can only be satisfied by 0C → 0N2.

Multiple independent solutions

There exist multiple solutions to your equation that are not simply multiples of each other. Your equation can be considered as two or more independent equations added together. For example, H + O → H2 + O2 has no unique solution because two solutions are 2H + 4O → H2 + 2O2 and 6H + 2O → 3H2 + O2, which are not multiples of each other. Furthermore, the equation can be separated as H → H2 and O → O2, each of which does have a unique solution.

Arithmetic overflow

Your equation used numbers that are too big, or a term has an element that occurs too many times, or the internal calculation used numbers that are too big. I don't expect this error to occur for real-world chemical formulas, only deliberately contrived ones. There is no workaround; the code would need to be rewritten to use bigints.

Assertion error

The author/programmer made a serious logic mistake. This error should not happen, but if it does please contact me.

Note

For simplicity of implementation, if the equation is successfully balanced but one or more terms have a negative coefficient, the program doesn't consider this outcome to be an error condition. In this case, each term that has a negative coefficient should be put on the other side of the equation, and its new coefficient should be the absolute value of the negative coefficient.