The GATE Grind

GATE 2025 CS (CS2) – Question 33

Programming and Data Structures · Programming in C · 1 mark · Numerical answer

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)

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 21

Explanation

This is the subtractive Euclidean algorithm, which computes gcd(126, 105) = 21.