Infosys Interview Question

Write the program for the given problem