Chemical Equation Balancer
Balance chemical equations automatically.
Use -> or = to separate reactants and products. Use + between compounds.
About this tool
A chemical equation balancer is an essential tool for chemists, students, and educators who need to ensure that chemical reactions are properly represented with equal numbers of atoms on both sides of the equation. Balancing equations by hand can be time-consuming and error-prone, especially for complex reactions involving multiple reactants and products. This tool automates the process, instantly finding the correct stoichiometric coefficients so you can focus on understanding the chemistry rather than wrestling with arithmetic.
To use the balancer, simply enter your unbalanced chemical equation in the input field using standard chemical notation—for example, H2 + O2 → H2O for the combustion of hydrogen. The tool parses your equation, identifies all unique elements and their counts, then calculates the minimal whole-number coefficients needed to balance it. Whether you're balancing simple reactions like iron rusting or complex organic synthesis, the tool handles single-step equations quickly and accurately.
This tool is most useful for high school and university chemistry students preparing for exams or completing homework, as well as educators who want to verify their work or generate example problems. Keep in mind that it balances individual equations—it doesn't predict reaction products or explain reaction mechanisms. For very large or unusual equations, always double-check the result against chemical principles to ensure the output makes sense for your specific context.
Frequently Asked Questions
Code Implementation
# Chemical equation balancing using linear algebra
import numpy as np
from fractions import Fraction
def parse_formula(formula):
"""Parse a chemical formula like H2O into {'H': 2, 'O': 1}"""
import re
pattern = r"([A-Z][a-z]?)([0-9]*)"
counts = {}
for element, count in re.findall(pattern, formula):
counts[element] = counts.get(element, 0) + int(count or 1)
return counts
def balance_equation(reactants, products):
"""
Balance a chemical equation.
reactants: list of formula strings, e.g. ["H2", "O2"]
products: list of formula strings, e.g. ["H2O"]
Returns coefficients for [reactants..., products...]
"""
# Collect all elements
all_compounds = reactants + products
all_elements = set()
for compound in all_compounds:
all_elements.update(parse_formula(compound).keys())
elements = sorted(all_elements)
# Build matrix: rows=elements, cols=compounds
# Reactants are positive, products are negative
n = len(all_compounds)
m = len(elements)
matrix = [[Fraction(0)] * n for _ in range(m)]
for j, compound in enumerate(all_compounds):
parsed = parse_formula(compound)
sign = 1 if j < len(reactants) else -1
for i, element in enumerate(elements):
matrix[i][j] = Fraction(sign * parsed.get(element, 0))
# Gaussian elimination to find null space
# (simplified — works for most single-reaction equations)
# For production use, consider sympy:
# from sympy import Matrix
# M = Matrix(matrix)
# coeffs = M.nullspace()[0]
return "Use sympy for robust balancing"
# Example with sympy (recommended)
from sympy import Matrix, lcm
from functools import reduce
def balance_sympy(reactants, products):
import re
all_compounds = reactants + products
elements = sorted(set(
e for f in all_compounds
for e, _ in re.findall(r"([A-Z][a-z]?)([0-9]*)", f)
))
rows = []
for elem in elements:
row = []
for i, f in enumerate(all_compounds):
counts = {e: int(n or 1) for e, n in
re.findall(r"([A-Z][a-z]?)([0-9]*)", f)}
sign = 1 if i < len(reactants) else -1
row.append(sign * counts.get(elem, 0))
rows.append(row)
M = Matrix(rows)
null = M.nullspace()[0]
denom = reduce(lcm, [v.q for v in null])
coeffs = [int(v * denom) for v in null]
return coeffs
coeffs = balance_sympy(["H2", "O2"], ["H2O"])
print(coeffs) # [2, 1, 2] -> 2H2 + O2 -> 2H2OComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.