recategorized by
3,614 views
11 votes
11 votes

Assume that $X$ and $Y$ are non-zero positive integers. What does the following Pascal program segment do?

while X <> Y do
if  X > Y then
    X := X - Y
else
    Y := Y - X;
write(X);
  1. Computes the LCM of two numbers

  2. Divides the larger number by the smaller number

  3. Computes the GCD of two numbers

  4. None of the above

recategorized by

3 Answers

3 votes
3 votes

Apply Option Elimination

take X=6,Y=4

if Ans will 12 then LCM , 2 then GCD, 1 then Divides the larger number by the smaller number

Now Trace the code

Iteration1 :-X=2,Y=4

            2:-X=2,Y=2

Print 2 ; which is GCD

take another pair X=12,Y=8 which gives 4 means ans is GCD .

Hence C is the Correct Ans

1 votes
1 votes
The algorithm is based on below facts.

If we subtract smaller number from larger (we reduce larger number), GCD doesn’t change. So if we keep subtracting repeatedly the larger of two, we end up with GCD.
Answer:

Related questions

21 votes
21 votes
1 answer
2
Kathleen asked Oct 8, 2014
3,987 views
In the following Pascal program segment, what is the value of X after the execution of the program segment?X := -10; Y := 20; If X Y then if X < 0 then X := abs(X) else ...
14 votes
14 votes
1 answer
4