An ionic compound has no overall charge, because the total positive charge of its ions equals the total negative charge. To work out a formula, write down the formula and charge of each ion, then find the smallest numbers of each ion that make the charges add up to zero.
Some ions to know: Na+, K+, Ag+, Mg2+, Ca2+, Zn2+, Cu2+, Al3+, Cl-, Br-, O2-, OH-, NO3-, CO32- and SO42-. The last four are groups of atoms that stay together, so brackets are used when more than one is needed.
Examples: Ca2+ and Cl- need two chloride ions for each calcium ion, so the formula is CaCl2. Mg2+ and NO3- need two nitrate ions, so the formula is Mg(NO3)2. Al3+ and O2- need two aluminium ions and three oxide ions, because 2 × 3+ equals 3 × 2-. The formula is Al2O3. Na+ and SO42- give Na2SO4.