Open In App

ISRO | ISRO CS 2018 | Question 43

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

Answer: (C)
Explanation:

For the above code consider A = 2 and B = 5:

Consider the given code :

1st pass :
while (A ! = B ){ If(A > B ) A -= B; else B – = A; B = B – A = 5 – 2 = 3 }
A = 2 and B = 3 

2nd pass :
while (A ! = B ){ If(A > B ) A – = B; else B – = A; B = B – A = 3 – 2 = 1 }
A = 2 and B =1

3rd pass :
while (A ! = B ){ If(A > B ) A – = B; A = A – B = 2 – 1 = 1 else B – = A;  }
A = 1 and B =1 After this, A = B = 1, while the condition becomes false. It prints the value of A as 1. (which is the GCD of 2 and 7).

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :