Skip to main content

GATE CSE 2019Programming in C › General C Programming ConceptsVerified

GATE CSE 2019 PYQ: Consider the following C program: #include <stdio.h> int main () { float sum = 0.0 , j = 1.0 , i = 2.0 ;…

Consider the following C program:

  1. #include <stdio.h>
  2. int main() {
  3. float sum = 0.0, j=1.0, i=2.0;
  4. while (i/j > 0.0625) {
  5. j=j+j;
  6. sum=sum+i/j;
  7. printf("%f\n", sum);
  8. }
  9. return 0;
  10. }

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

    Answer

    5

    Approach

    Trace the while loop carefully: the condition is tested before each iteration, so we count only the printf executions that occur inside iterations where i/j &gt; 0.0625. The variables are floats, but 0.0625 = $2^{-4}$ is exactly representable, so there is no rounding ambiguity.

    The loop prints sum exactly 5 times before the strict inequality fails at i/j = 2/32 = 0.0625.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.