C solution for 6.