Comments have been written for all the codes for better understanding