Solved Problem 2 in C