How many lines of output does the following C code produce? C#include<stdio.h>…

2014

How many lines of output does the following C code produce? 

C
#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);
    }
}
  1. A.

    8

  2. B.

    9

  3. C.

    10

  4. D.

    11

Attempted by 384 students.

Show answer & explanation

Correct answer: D

The code initializes i = 2.0 and j = 1.0. The loop condition i/j > 0.001 simplifies to j < 2000. Inside the loop, j doubles each iteration (j += j). The values of j are powers of 2: 1, 2, 4, ..., 1024. Since 1024 < 2000, the loop executes for this value. The next value would be 2048, which exceeds 2000, stopping the loop. Counting from 2^0 to 2^10 gives exactly 11 iterations.

Explore the full course: Isro