The GATE Grind

GATE 2019 CS – Question 62

Programming and Data Structures · Programming in C · 2 marks · Numerical answer

Consider the following C program:

#include <stdio.h>
int main(){
    float sum = 0.0, j = 1.0, i = 2.0;
    while (i/j > 0.0625){
        j = j + j;
        sum = sum + i/j;
        printf("%f\n", sum);
    }
    return 0;
}

The number of times the variable sum will be printed, when the above program is executed, is ________.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 5

Explanation

Before each iteration the test is $\frac{2}{j}>0.0625$, i.e. $j<32$. The value of $j$ is 1, 2, 4, 8, 16 at the start of the five iterations that run, and then $j$ becomes 32 and the loop stops. So `sum` is printed 5 times.