GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Identify or trace subtractive Euclid to equality

3 questions solved the same way, newest first. Back to Programming in C

How these are solved

Repeatedly subtract the smaller positive integer from the larger, preserving gcd, until equality (or an explicit1 shortcut). The 2004 question identifies the general function;

1 mark · NAT
Report issue

Control Flow & Loop Tracing

Question 33, GATE 2025: Find the integer printed by this C fragment:
int x=126,y=105;
do { if(x>y) x=x-y; else y=y-x; } while(x!=y);
printf("%d",x);
Tap to enlarge
Answer & solution

Official answer: 21

Step through it (21 steps)

Trace the pair (x,y) after each execution of the body:
(126,105) → (21,105) → (21,84) → (21,63) → (21,42) → (21,21).
At the first step x>y, so only x changes. Thereafter y>x until equality is reached, so y is reduced repeatedly by 21. The loop test sees x==y after the fifth iteration and stops. The printed x is 21.

This is the subtraction version of Euclid's algorithm. Replacing a larger positive integer by its difference with the smaller preserves the set of common divisors, and hence the gcd. At termination the two equal positive values must equal gcd(126,105)=21. Remember that a do-while executes the body before testing its condition; here the initial values differ, so that distinction does not alter the trace. Answer: 21.

Original paper, page 26 · Answer key

1 mark · NAT
Report issue

Recursion

Question 23, GATE 2021: SomeFunction(x,y): if x==1 or y==1 return 1; if x==y return x; if x>y recurse on (x−y,y); if y>x recurse on (x,y−x). Find SomeFunction(15,255).
Tap to enlarge
Answer & solution

Official answer: 15

Step through it (19 steps)

Each recursive step subtracts the smaller positive argument from the larger, preserving their gcd. Starting at(15, 255), repeated subtraction reaches(15, 15), which returns 15. Neither argument reaches 1 on this input.

The invariant gcd(x,y)=gcd(x,y−x) explains correctness, while decreasing x+y proves termination for positive integers. Here y successively becomes 240,225,…,30,15, after sixteen subtractions of 15, and the equality branch returns 15. A modulo-based Euclidean algorithm would skip these repeated subtraction calls.

Original paper, page 65 · Answer key

2 marks · MCQ
Report issue

Subtractive Euclidean algorithm

Control Flow & Loop Tracing

Question 41, GATE 2004: What does this C-style fragment compute for positive integers x,y?
m=x; n=y;
while(m!=n) {
  if(m>n) m=m-n;
  else n=n-m;
}
printf("%d",n);
GFG option A is division by repeated subtraction; GateForum prints x+y instead.
Options:
(A) x/y by repeated subtraction
(B) x mod y by repeated subtraction
(C) Greatest common divisor of x,y
(D) Least common multiple of x,y
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. For positive x,y, each iteration replaces the larger of m,n by its difference with the smaller. This preserves gcd(m,n), keeps both positive, and strictly decreases m+n until m=n.

2. The printed common value is gcd(x,y). This is not ordinary repeated subtraction for division: the roles of the two operands change.

3. Teaching check: For x=12,y=8, the pairs evolve (12,8)→(4,8)→(4,4), so the answer is 4. Every common divisor is preserved by subtracting one operand from the other. The positive decreasing sum proves termination, completing both partial correctness and progress.

Original paper, page 6