retagged by
12,560 views
21 votes
21 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 _________

retagged by

3 Answers

Best answer
34 votes
34 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
7 votes
7 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.

 

Answer:

Related questions

18 votes
18 votes
2 answers
1
Arjun asked Feb 7, 2019
15,358 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"...
59 votes
59 votes
9 answers
2
Arjun asked Feb 7, 2019
27,053 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...
14 votes
14 votes
6 answers
3
Arjun asked Feb 7, 2019
13,420 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 numb...
13 votes
13 votes
4 answers
4
Arjun asked Feb 7, 2019
9,567 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"...