Consider the following program fragment i=6720; j=4; while (i%j)==0 { i=i/j;…

2015

Consider the following program fragment i=6720; j=4; while (i%j)==0 { i=i/j; j=j+1; } On termination j will have the value

  1. A.

    4

  2. B.

    8

  3. C.

    9

  4. D.

    6720

Attempted by 396 students.

Show answer & explanation

Correct answer: C

The program initializes i to 6720 and j to 4. The loop continues dividing i by j as long as the remainder is zero. Tracing execution: 6720/4=1680 (j becomes 5), 1680/5=336 (j becomes 6), 336/6=56 (j becomes 7), 56/7=8 (j becomes 8), 8/8=1 (j becomes 9). At j=9, i is 1, and 1%9 is not zero. The loop terminates with j equal to 9.

Explore the full course: Isro