recategorized by
5,725 views
15 15 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

3 Answers

5 5 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

3 3 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:
Position:
Show:

Related questions

17 17 votes
3 answers 3 answers
5.5k
5.5k views
Kathleen asked Oct 8, 2014
5,502 views
Consider the following Pascal function where $A$ and $B$ are non-zero positive integers. What is the value of $\text{GET}(3, 2)$?function GET(A,B:integer): integer; begin...
24 24 votes
1 answers 1 answer
8.2k
8.2k views
Kathleen asked Oct 8, 2014
8,160 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 ...
39 39 votes
3 answers 3 answers
9.6k
9.6k views
Kathleen asked Oct 8, 2014
9,623 views
Consider the following high level programming segment. Give the contents of the memory locations for variables $W, X, Y$ and $Z$ after the execution of the program segmen...
24 24 votes
1 answers 1 answer
6.1k
6.1k views
Kathleen asked Oct 8, 2014
6,068 views
A language with string manipulation facilities uses the following operationshead(s): first character of a string tail(s): all but exclude the first character of a stringc...