ICSE Class 10 Computer Applications
Question 22 of 30
Solved Sample Paper 2 — Question 22
Back to all questions 22
Question Question 2(ii)
Observe the following code and write how many times will the loop execute?
a = 5;
b = 2;
while(b != 0)
{
r = a % b;
a = b;
b = r;
}
System.out.println(" " + a);The loop will execute 2 times.
Explanation
| Iteration | r | a | b | Remark |
|---|---|---|---|---|
| 5 | 2 | Initial values | ||
| 1 | 1 | 2 | 1 | r = 5 % 2 = 1 |
| 2 | 0 | 1 | 0 | r = 2 % 1 = 0 Loop terminates as b = 0 |