Skip to content
Official PYQ · 2019

GATE CS 2019 Q52 — Programming in C

Explain with AI

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 _________

View worked solution

Answer: 5

We can trace the execution of the loop step-by-step. Note that the loop condition is ij>0.0625\frac{i}{j} > 0.0625, where 0.0625=1160.0625 = \frac{1}{16}. Initially, i=2.0i = 2.0, j=1.0j = 1.0, and sum=0.0\text{sum} = 0.0.

IterationCondition Check (ij>0.0625\frac{i}{j} > 0.0625)New jj (j=j+jj = j + j)New sum\text{sum} (sum+ij\text{sum} + \frac{i}{j})Printed?
12.01.0=2.0>0.0625\frac{2.0}{1.0} = 2.0 > 0.0625 (True)2.02.01.01.0Yes (1st)
22.02.0=1.0>0.0625\frac{2.0}{2.0} = 1.0 > 0.0625 (True)4.04.01.51.5Yes (2nd)
32.04.0=0.5>0.0625\frac{2.0}{4.0} = 0.5 > 0.0625 (True)8.08.01.751.75Yes (3rd)
42.08.0=0.25>0.0625\frac{2.0}{8.0} = 0.25 > 0.0625 (True)16.016.01.8751.875Yes (4th)
52.016.0=0.125>0.0625\frac{2.0}{16.0} = 0.125 > 0.0625 (True)32.032.01.93751.9375Yes (5th)
62.032.0=0.0625>0.0625\frac{2.0}{32.0} = 0.0625 > 0.0625 (False)--No (Loop terminates)

Thus, the printf statement inside the loop is executed exactly 5 times.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions