Open In App
Related Articles

ISRO | ISRO CS 2014 | Question 38

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

C

#include 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
Please comment below if you find anything wrong in the above post

Last Updated : 16 May, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads