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);
Answer

The loop will execute 2 times.

Explanation
IterationrabRemark
  52Initial values
1121r = 5 % 2 = 1
2010r = 2 % 1 = 0
Loop terminates as b = 0