Solved problem 6 in C#.