Programmed way of solving second problem.