Code for problem 4.