Skip to content
Related Articles

Related Articles

ISRO | ISRO CS 2014 | Question 38
  • Last Updated : 16 May, 2018

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);
    }
}

(A) 8
(B) 9
(C) 10
(D) 11


Answer: (D)

Explanation: Given, i = 2.0 and j = 1.0.
Since “i” is not changing, to satisfy condition:

 i/j > 0.001 
→ 2.0/j > 0.001
→ 2.0/0.001 > j
→ 2000 > j 

Value of “j” should be less than 2000 to satisfy given condition. Value of “j” is increasing in power of 2, i.e., 2n. So,

 2n > 2000
→ n > 10.9657
→ n = 11 

So, after 11th line while loop will terminate.
Actual output is:

1.000000
1.500000
1.750000
1.875000
1.937500
1.968750
1.984375
1.992188
1.996094
1.998047
1.999023

These are total 11 line.

So, option (D) is correct.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :