Solved problem16 in java