How many lines of output does the following C code produce?
#include <stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
while (i/j > 0.001)
{
j+=j;
sum=sum+(i/j);
printf("%f\n", sum);
}
}
Answer: D
Correct answer: 11
Each pass doubles j and prints, so the tested quantity \(i/j\) starts at 2 and halves every time: 2, 1, 0.5, 0.25, … The loop continues while it exceeds 0.001. Since \(2/2^{11} = 0.000977\) is the first value at or below the threshold, the test succeeds for \(j = 1, 2, 4, \ldots, 1024\) - eleven passes, and eleven lines.
Please log in or register to report this question.