Solves Problem 10 in C++