Look at the periodic table to find the charge on atoms.
Magnesium is +2 and Nitrogen is -3. Since there are two nitrogen charge 2*-3 = -6 there needs to be 3 Mg then (3*2+ = 6+) to pair with the two nitrogen.
3 Mg(+2) + 2 N(-3) = Mg3N2
Answer:
A. Generally ionic compounds are formed metal and non metal elements since they forms opposite charged ions due to difference in their Electronegativity values.
NaCl, MgCl2, Ca(OH)2 etc are ionic compounds.
B. Generally transition elements (D elements) forms cations with different charges or simply they exist in different oxidation states due to availability of empty d orbitals.
Examples are Iron exsits as Fe+2 and Fe+3
Mn exists as Mn+2 , Mn+4, Mn+6 ,Mn+7 etc.