Answer:
Following is given the code according to requirement.
The code is attached as an image so that the indentation is understood by the user. Comments are given inside the code where necessary.
The output of code is also attached as well in end.
I hope it will help you!
Step-by-step explanation: