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)
View worked solution
Answer: 21:21
The given C code segment implements the subtraction-based Euclidean algorithm to compute the Greatest Common Divisor (GCD) of two positive integers and .
Let's trace the execution of the do-while loop step-by-step with initial values and :
- Iteration 1:
- Since (), we update .
- Now, and .
- Loop condition check: () is
true. Loop continues.
- Iteration 2:
- Since () is
false, we update . - Now, and .
- Loop condition check: () is
true. Loop continues.
- Since () is
- Iteration 3:
- Since () is
false, we update . - Now, and .
- Loop condition check: () is
true. Loop continues.
- Since () is
- Iteration 4:
- Since () is
false, we update . - Now, and .
- Loop condition check: () is
true. Loop continues.
- Since () is
- Iteration 5:
- Since () is
false, we update . - Now, and .
- Loop condition check: () is
false. The loop terminates.
- Since () is
After the loop terminates, the printf statement prints the value of , which is 21 (which is indeed ).
Free practice · Your answers on this page are not saved.
