First you need to write yourself a balanced equation. So,
2HCl + Mg —> 2H2 + MgCl2
So, we start with our 40g HCl and we’re converting to H2. We know that the molar mass of HCl is 36.5 and the molar mass of H2 is 2
So we convert HCl in grams to HCl in mols
40/36.5= 1.1 mol HCl.
Now we have to convert our mols of HCl into mols of H2. Looking at our balances equation, We know that the ratio is 2 to 2. Easy enough.
Now we have 1.1 mol H2, which we need to convert back to grams.
All we have to do now is multiply our mols by the molar mass and we get 2.2g H2 gas