Solves Problem 9 in C++