please see the logic and where error occurs
hi @vmayaan, corrected and commented https://ide.codingblocks.com/s/662088