GateAI
Programming in C

GATE CSE 2025 Set 2 · Q33

NAT1 markEasy

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

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 21

Programming in C

The given code implements the Euclidean algorithm for computing the greatest common divisor (GCD) of two numbers using repeated subtraction.

Initially:

  • x=126x = 126, y=105y = 105

Trace of the loop:

  1. x>yx > y (126>105126 > 105)   ⟹  x=126−105=21,y=105\implies x = 126 - 105 = 21, y = 105

  2. x<yx < y (21<10521 < 105)   ⟹  y=105−21=84,x=21\implies y = 105 - 21 = 84, x = 21

  3. x<yx < y (21<8421 < 84)   ⟹  y=84−21=63,x=21\implies y = 84 - 21 = 63, x = 21

  4. x<yx < y (21<6321 < 63)   ⟹  y=63−21=42,x=21\implies y = 63 - 21 = 42, x = 21

  5. x<yx < y (21<4221 < 42)   ⟹  y=42−21=21,x=21\implies y = 42 - 21 = 21, x = 21

  6. x=y=21x = y = 21, loop terminates.

Then, printf("%d", x); prints 21.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

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 C

More from GATE CSE 2025 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 2 · Q33 · NAT

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.