Completed problem 3 in C, solution could be better