Solves Problem 16 in C++