Computer Applications

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

Java Iterative Stmts

11 Likes

Answer

The loop will execute 2 times.

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

Answered By

6 Likes


Related Questions