Code as per the question