- 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)
GATE CSE 2025Programming in C › Recursion & Program TracingVerified
21
The do-while loop repeatedly replaces the larger of x and y by its difference with the smaller; this is the subtraction form of the Euclidean algorithm, so the final common value is gcd(126,105). Direct tracing gives the same result.
The loop computes gcd(126,105) by repeated subtraction. Since gcd(126,105)=21, and the loop terminates when x and y both equal this gcd, the printed value of x is 21.
Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.