Code for problem 3