Assume that p and q are non-zero positive integers, the following program…

2022

Assume that p and q are non-zero positive integers, the following program segment will perform – while (p != q) { if (p > q) p = p - q; else q = q - p; } printf("%d", p);

  1. A.

    Subtract small number from large number

  2. B.

    Compute GCD of the given numbers

  3. C.

    Compute LCM of the given numbers

  4. D.

    The loop will run infinitely

Attempted by 157 students.

Show answer & explanation

Correct answer: B

The code implements the Euclidean Algorithm via repeated subtraction to calculate the Greatest Common Divisor (GCD) of p and q.

Explore the full course: Rssb Senior Computer Instructor