1,103 views
0 votes
0 votes

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

#include<stdio.h>
main()
{ float sum=0.0,j=1.0,i=2.0;
    while (i/j > 0.001)
    {
        j=j+1;
        sum=sum+(i/j);
        printf("%f\n", sum);
    }
}
  1. 0-9 line of output
  2. 10-19 lines of output
  3. 20-29 lines of output
  4. More than 29 lines of output

2 Answers

3 votes
3 votes
Option 4.

Check condition while (i/j > 0.001) according to options:

1. For line 10: 2/10 > 0.001 is True thus option 1 is False.

2. For line 20: 2/20> 0.001 is True thus option 2 is False.

3. For line 30: 2/30 > 0.001 is True thus option 3 is False.

Thus, the program prints more than 29 lines of output.
edited by
0 votes
0 votes
we have to just check the value of i/j. In each while loop, j is incremented by 1 and i will be always 2.0 .so there may be 4 cases

CASE 1: IF OUTPUT IS 10 LINES

it means the value of j after 10 iterations will be 11.

so i/j=2.0/11=0.18>0.001(while loop will not break)

CASE 2:IF OUTPUT IS 20 LINES

J=21

I/J=2.0/21=0.09>0.001(while loop will not break)

CASE 3: IF OUTPUT  IS OF 30 LINES

J=31

I/J=2.0/31=0.06>0.001(while loop will not break)

SO ANSWER WILL BE D.

Related questions

1 votes
1 votes
1 answer
1
Rakshita Jadoun asked Feb 4, 2023
624 views
#include <stdio.h int main() {int i,j,x;scanf("%d ",x);i=1;j=1;while(i<10){j=j*i;i=i+1;if(i==x) break;}return 0;}
1 votes
1 votes
1 answer
3
nehavora asked Aug 16, 2017
298 views
0 votes
0 votes
0 answers
4
eyeamgj asked Nov 27, 2018
160 views
https://gateoverflow.in/54924/calculate-minimum-packet-sizeWHAT IS THE MEANING OF QUESTION ?