• edited by
10,467 views
8 8 votes

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);
    }
}
  1. 8
  2. 9
  3. 10
  4. 11

3 Answers

Best answer
19 19 votes

Answer is 11

initial condition j = 1 and i =2

i/j>0.001 ..... j should be >2000

now j increases as 2n 

So 2n > 2000 i.e 2048

therefor n = 11

for n = 12.... i/j<0.001 so condition false and exits loop

• selected by
3 3 votes
Its very simple maths used here

i/j > 0.001 holds true

As you can see i=2 always and never changes , while the value of j is constantly changing in terms of 2^n.

2/2^n > 0.001

1/2^(n-1) > 0.001

2^(n-1) < 1000 holds true

it will fail when 2^(n-1)=1024 ,

hence n-1=10

n=11
0 0 votes
Answer is D
1 flag:
✌ Low quality (shriverdhan)
Answer:
Position:
Show:

Related questions

9 9 votes
8 answers 8 answers
14.6k
14.6k views
ajit asked Sep 2, 2015
14,553 views
What is the output of the following C program?#include<stdio.h void main(void){ int shifty; shifty=0570; shifty=shifty>>4; shifty=shifty<<6; printf("The value of shifty i...
11 11 votes
7 answers 7 answers
13.3k
13.3k views
sh!va asked May 7, 2017
13,254 views
What will be the output of the following C code?#include <stdio.h main() { int i; for(i=0;i<5;i++) { int i=10; printf("%d" , i); i++; } return 0; }10 11 12 13 1410 10 10 ...
0 0 votes
1 1 answer
1.0k
1.0k views
TusharKumar asked Dec 22, 2022
1,006 views
can anyone explain how the for loop works here..?