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)
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2025 Set 2 · Q33
Time
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)Your answerNumerical answer
Solution
Official answer: 21
Programming in CThe given code implements the Euclidean algorithm for computing the greatest common divisor (GCD) of two numbers using repeated subtraction.
Initially:
,
Trace of the loop:
()
()
()
()
()
, loop terminates.
Then, printf("%d", x); prints 21.
Try the question first, then check your work.
Examiner trap
The loop repeatedly subtracts the smaller value from the larger value until both variables become equal, which calculates the GCD of 126 and 105.
This Programming in C previous year question is Q33 of the GATE CSE 2025 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 16 of them are 1-mark NAT questions like this one, and GATE CSE 2025 Set 2 has 4.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.