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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 May, 2018
Like Article
Save Article