Computer Science, asked by viruyadavacet, 1 year ago

How many times does the while loop get executed if the following function is called as f(190,15)?
f(m,n) {
ans := 1
while (m - n >= 0) {
ans := ans * 2
m := m - n
}
return(ans)
}

Answers

Answered by topanswers
4

Answer: 12 times

Because there is change happening in the value contained in variable 'm' inside the loop.

The value is depreciated by value 15 (contained in the variable 'n'). Thus, each time the loop executes, the condition is checked by taking the value stored in the ‘m’ and ‘n’ variable.

Thus the final value after depreciation, of ‘m’ is 10, whereas, the original value was 190.

Thus, subtracting new value of ‘m’ from the original value of ‘m’:

Mo - Mf = 190 – 10 = 180, so, the value 180 is a multiple of 15. Reverse logic holds correct.

m = 190, n = 15

m = 175, n = 15

m = 160, n = 15

m = 145, n = 15

m = 130, n = 15

m = 115, n = 15

m = 100, n = 15

m = 85, n = 15

m = 70, n = 15

m = 55, n = 15

m = 40, n = 15

m = 25, n = 15

Final m = 10, n = 15

Thus the loop runs 12 times.

Answered by niravgamit012
0

Answer:

13

Explanation:

15*12=180 so it counts 12 and if you count partial execution  of while loop which will find m-n in less than zero than answer will be 13.

Similar questions