How to calculate molarity of a solution by solving the percentage?
Both of them are pretty straightforward. Don't go by the formulae, but instead, internalize the steps required here.

$2\%$ w/v solution has $\pu{2g}$ of solute in $\pu{100 ml}$ solution. That implies $\pu{20g}$ of solute in $\pu{1L}$ solution. $\pu{20g}$ solute is 0.5 moles of $\ce{NaOH}$ (why?). So, molarity is simply moles divided by volume of solution.

The $2\%$ w/w solution case is trickier, and you'll also be given the density of solution in this case.  $2\%$ w/w solution has $\pu{2g}$ solute in $\pu{100g}$ solution, or $\pu{20g}$ solute in $\pu{1000g}$ solution. If the density is $\pu{2gm L^-1}$ (assumed), then the volume of solution would be $\ce{500 mL}$. Now, you can proceed by the same logic as above.

In fact, the conversion of $\pu{100ml}$ volume to $\pu{1L}$ in case 1 wasn't even necessary, but I only did that to make the step-by-step calculations more approachable for you.