Code for problem 18.