Solved problem 5 in C#.