Problem 3 solution in c