ISRO | ISRO CS 2014 | Question 38

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.