Question:medium

Consider the following C code segment:

int x = 126, y = 105;
do {
    if (x > y)
        x = x - y;
    else
        y = y - x;
} while (x != y);

printf("%d", x);

The output of the given C code segment is ____________. (Answer in integer)

Show Hint

The given C code implements the Euclidean algorithm to compute the greatest common divisor (GCD) of two numbers.
Updated On: Jan 30, 2026
Show Solution

Correct Answer: 21

Solution and Explanation

The two numbers can be broken down into their prime factors. The common factors shared by both values determine their greatest common divisor.

\(126 = 2 \times 3^2 \times 7\)
\(105 = 3 \times 5 \times 7\)

The factors common to both numbers are \(3\) and \(7\). Multiplying these gives:

\(3 \times 7 = 21\)

Hence, the greatest common divisor of \(126\) and \(105\) is \(\boxed{21}\).

Was this answer helpful?
0