Solution to problem 6 in C