The Gateway to Computer Science Excellence
+5 votes
2.6k 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 by Veteran (431k points)
edited by | 2.6k views
+7
5 will be answer
+4

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.

3 Answers

+14 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.
by Veteran (60.8k points)
edited by
+1
Typo  $2/32$
+3 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.

 

by Active (1.5k points)
0 votes

5 times while loop will iterate..

by Active (3k points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,279 answers
198,173 comments
104,840 users