So here is the code for finding average of student's marks and find average if the user enters -1 but I am not getting why we have to add 1 in total and then subtract 1 from i .
Notice that the while statement stops when x has -1. We don't want -1 to be part of the total or average, we just used -1 to stop taking input.
So by writing total+1 we eliminate the -1 we had added earlier. And we also decrement the i variable by 1 because we didn't consider the x=-1 as an input. ;)