Consider a following piece of C Programming code: Int x =128, y=110; do {…
2025
Consider a following piece of C Programming code:
Int x =128, y=110;
do
{
if(x>y)
x=x-y;
else
y=y-x;
}while(x!=y)
printf(“%d”,x);
Which one will be the output?
- A.
18
- B.
2
- C.
92
- D.
74
Attempted by 190 students.
Show answer & explanation
Correct answer: B
Key idea: the code computes the greatest common divisor (GCD) of x and y using repeated subtraction (the Euclidean algorithm by subtraction).
Start: x = 128, y = 110.
Since x > y, set x = x - y → x = 128 - 110 = 18 (now x = 18, y = 110).
Continue subtracting the smaller from the larger:
y sequence: 110 → 92 → 74 → 56 → 38 → 20 → 2 (after repeatedly subtracting x = 18).
x sequence then reduces: 18 → 16 → 14 → 12 → 10 → 8 → 6 → 4 → 2 (after subtracting y = 2 repeatedly).
When both values become equal (x = y = 2) the loop stops and printf prints that value.
Final answer: 2
A video solution is available for this question — log in and enroll to watch it.