How many times is the comparison i ≥ n performed in the following program?

int i=85, n=5;
main() {
while (i >= n) {
i=i-1;
n=n+1;
}
}

 

40

41

42

43

Responses

sumitverma's picture

The value of i − n is 80 initially. We run the loop as long as i − n ≥ 0 and in each iteration, i − n decreases by 2. Just before the kth time the comparison is performed (for k ≥ 1), the value of i−n is 80−2k+2. Hence just before the forty-first comparison, the value of i − n is 0. After the forty-first comparison, the loop is executed one last time. We need to make the comparison once more to exit the loop. Thus the correct answer is 42.

Did not found what you are looking for, Ask your doubt or Help by your contribution

Enter your search keyword:

Search form

Wait!

Here is a chance to join biggest community of technical Students,
Tutors with FREE learning resources and so much more.
It takes less then 60 seconds.