Code for problem 6.