Magnesium chloride:
The charge on magnesium ion is +2 and chloride ion has -1. So, the chemical formula of magnesium chloride is MgCl2.
Calcium carbonate:
The charge on calcium cation is +2 and the carbonate anion -2. So, the chemical formula of calcium carbonate is CaCO3.