search
Log In
6 votes
3.8k views

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 _________

in Programming
edited by
3.8k views
7
5 will be answer
5

Initially

i = 2.0, j = 1.0

J = 1 => i/j = 2/1  > 0.0625 // First print

J = 1+1 => i/j = 2/2 > 0.0625 //second print

J = 2+2 => i/j = 2/4  > 0.0625// Third print

J = 4+4 => i/j = 2/8 > 0.0625 // fourth print

J = 8+8 => i/j = 2/16 > 0.0625 // Fifth print

J = 16+16 => i/j = 2/32 > 0.0625 (False)

 

0
$\mathbf{5}$ times.

4 Answers

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

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

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

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

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

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

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

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

Total $5$ times sum will be printed.

edited by
1
Typo  $2/32$
5 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.

 

4 votes

5 times while loop will iterate..

1 vote

Hope this helps :)

Answer:

Related questions

4 votes
5 answers
1
4k 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",x); return 0; } The value printed by the program is ______________.
asked Feb 7, 2019 in Programming Arjun 4k views
6 votes
7 answers
2
4k 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[1]); return 0; } The number that will be displayed on execution of the program is _______
asked Feb 7, 2019 in Programming Arjun 4k views
5 votes
3 answers
3
4.6k 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", sum); return 0; } The output of the above C program is _______
asked Feb 7, 2019 in Programming Arjun 4.6k views
22 votes
10 answers
4
7.9k 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 following values will be displayed on execution of the programs? $41$ $52$ $63$ $630$
asked Feb 7, 2019 in Programming Arjun 7.9k views
...