Solution in C