Adding solution for problem 40 in c.