Assume A and B are non-zero positive integers. The following code segment:…
2018
Assume A and B are non-zero positive integers. The following code segment:
while ( A != B){
if ( A > B ) A - = B ;
else B - = A ;
}
cout << A; // printing the value of A
- A.
Computes the LCM of two numbers
- B.
Divides the larger number by the smaller number
- C.
Computes the GCD of two numbers
- D.
Finds the smaller of two numbers
Attempted by 408 students.
Show answer & explanation
Correct answer: C
The code implements the subtraction-based Euclidean algorithm to find the Greatest Common Divisor (GCD) of two positive integers A and B.
In each iteration of the while loop, the larger value is reduced by subtracting the smaller value. This process continues until both variables hold the same value.
When the loop terminates (A == B), this common value represents the GCD of the original inputs.
Therefore, the code prints the GCD of A and B.