Computer Science, asked by sonaisona1998, 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 aishwaryaprakashv
0

12 times.

source nptel ADA quiz 1.


Answered by omegads03
0

The following are the iterations when the function f(190, 15) is called.

m=190, n=15, ans=1

1st iteration:

m-n >= 0

i.e. 190-15 = 175 >= 0 True

So, ans = ans × 2 i.e. ans = 1 × 2 = 2

m = 175

2nd iteration:

175-15 = 160 >= 0 True

So, ans = 2 × 2 = 4

m = 160

3rd iteration:

160-15 = 145 >= 0 True

So, ans = 4 × 2 = 8

m = 145

.

.

.

.

.

.

12th iteration:

25-15 = 10 >=0 True

So, ans = 2048 × 2 = 4096

m = 25

13th iteration:

10-15 = -5 >=0 False

So, the loop will stop here.

Therefore the while loop will execute 12 times.

Similar questions