• retagged by
25,567 views
41 41 votes

Consider the following C program:

#include <stdio.h>
int main() {
    float sum = 0.0, j=1.0, i=2.0;
    while (i/j > 0.0625) {
        j=j+j;
        sum=sum+i/j;
        printf("%f\n", sum);
    }
    return 0;
}

The number of times the variable sum will be printed, when the above program is executed, is _________

6 Answers

Best answer
64 64 votes
$i = 2.0, j=1.0$

while $\left( \frac{i}{j} > 0.0625\right)$

$j = 1$
$ \frac{i}{j} = \frac{2}{1}  > 0.0625$
$j=j+j, 1^{\text{st}}$ PRINT

$j=2$
$ \frac{i}{j} = \frac{2}{2}  > 0.0625$
$j=j+j, 2^{\text{nd}}$ PRINT

$j=4$
$ \frac{i}{j} = \frac{2}{4}  > 0.0625$
$j=j+j, 3^{\text{rd}}$ PRINT

$j=8$
$ \frac{i}{j} = \frac{2}{8}  > 0.0625$
$j=j+j, 4^{\text{th}}$ PRINT

$j=16$
$ \frac{i}{j} = \frac{2}{16}  > 0.0625$
$j=j+j, 5^{\text{th}}$ PRINT

$j=32$
$ \frac{i}{j} = \frac{2}{32}  = 0.0625$
$\text{Break}$

Total $5$ times sum will be printed.
• edited by
15 15 votes

while loop condition is :-

$\frac{2}{2^{k}}> 0.0625$

this condition will be true for k=0,1,2,3,4 but fails for k=5

so it'll print sum 5 times.

 

0 0 votes

 

 

sum = 0.0
i = 2.0
j = 1.0
IterationCurrent j ValueCondition Check: 2/j > 1/16Is it True?Action Inside Loop
1st1.02/1 = 2 > 0.0625Yesj becomes 2.0, prints sum (1st time)
2nd2.02/2 = 1 > 0.0625Yesj becomes 4.0, prints sum (2nd time)
3rd4.02/4 = 0.5 > 0.0625Yesj becomes 8.0, prints sum (3rd time)
4th8.02/8 = 0.25 > 0.0625Yesj becomes 16.0, prints sum (4th time)
5th16.02/16 = 0.125 > 0.0625Yesj becomes 32.0, prints sum (5th time)
6th32.02/32 = 0.0625 > 0.0625NoLoop terminates immediately

Final Answer: 5

0 0 votes

Firstly, for simplicity convert 0.0625 into 1/16.

0.0625 = 625/10000 = 1/16

The loop executes while i/j > 1/16. Here, i always remains 2 and j is doubled (j = j + j) after every iteration. Hence, the value of i/j is halved in each iteration. Since the question only asks how many times sum is printed, we only need to check the loop condition.

Initially: i = 2, j = 1

(1) 2/1 > 1/16 → True → Print

↓

(2) 2/2 > 1/16 → True → Print

↓

(3) 2/4 > 1/16 → True → Print

↓

(4) 2/8 > 1/16 → True → Print

↓

(5) 2/16 > 1/16 → True → Print

↓

2/32 > 1/16 → False

Loop terminates.

Hence, sum is printed 5 times.

Answer:
Position:
Show:

Related questions

44 44 votes
2 answers 2 answers
23.3k
23.3k views
Arjun asked Feb 7, 2019
23,324 views
Consider the following C program:#include <stdio.h int main() { int a[] = {2, 4, 6, 8, 10}; int i, sum=0, *b=a+4; for (i=0; i<5; i++) sum=sum+(*b-i)-*(b-i); printf("%d\n"...
38 38 votes
10 answers 10 answers
23.5k
23.5k views
Arjun asked Feb 7, 2019
23,515 views
Consider the following C program:#include <stdio.h int main() { int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4; printf(“%d\n”, ip ); return 0; }The number t...
107 107 votes
11 answers 11 answers
43.7k
43.7k views
Arjun asked Feb 7, 2019
43,672 views
Consider the following C program:#include <stdio.h int r() { static int num=7; return num ; } int main() { for (r();r();r()) printf(“%d”,r()); return 0; }Which one of the...
36 36 votes
5 answers 5 answers
16.8k
16.8k views
Arjun asked Feb 7, 2019
16,814 views
Consider the following C program :#include<stdio.h int jumble(int x, int y){ x = 2*x+y; return x; } int main(){ int x=2, y=5; y=jumble(y,x); x=jumble(y,x); printf("%d \n"...