Infosys Interview Question

To write the code in java.