Code for problem 6, but not correct answer