Skip to main content

GATE CSE 2025Programming in C › Recursion & Program TracingVerified

GATE CSE 2025 PYQ: int x = 126 , y = 105 ; do { if ( x > y ) x = x - y ; else y = y - x ; } while ( x != y ); printf ( "%d" , x…

  1. int x=126,y=105;
  2. do {
  3. if(x>y) x=x-y;
  4. else y=y-x;
  5. } while(x!=y);
  6. printf("%d",x);

The output of the given C code segment is _________. (Answer in integer)

    Answer

    21

    Approach

    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.