C solution for problem 12.