To get the percent composition of an element in a compound, simply divide the mass of the element by the total mass of the compound. And since we're going to need to calculate the percentage a couple of times, let's first start by calculating the total mass of the compound:
M = 12.2 g + 5.16g = 17.36 g
Now the percentage of magnesium:
12.2 g / 17.36 g = 0.702764977 = 70.3%
And the percentage of nitrogen:
5.16 g / 17.36 g = 0.297235023 = 29.7%
Magnesium = 70.3%
Nitrogen = 29.7%