Solve problem 50 in C++