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); Computes the LCM of two numbers Divides the larger number by the smaller number Computes the GCD of two numbers None of the above Algorithms gate1995 algorithms identify-function normal + – Kathleen asked Oct 8, 2014 • recategorized Apr 25, 2021 by Lakshman Bhaiya Kathleen 3.6k views answer comment Share Follow See all 3 Comments See all 3 3 Comments reply bhuv commented Jan 5, 2018 reply Follow Share while X <> Y && (X!=0 || Y!=0) do // I don't know if '&&' works in pascal if X > Y then //Even if this considered as comment or not. X := X - Y else Y := Y - X; write(X); 0 votes 0 votes Kiyoshi commented Jun 5, 2021 reply Follow Share if anyone is struggling with <> . it is nothing but not equal to in Pascal, means if we write x<>y it is true if x is not equal to y. you can equate with x!=y in C. 0 votes 0 votes madhes23 commented Oct 25, 2021 reply Follow Share Similar question – https://gateoverflow.in/1038/gate-cse-2004-question-41 0 votes 0 votes Please log in or register to add a comment.
Best answer 20 votes 20 votes Answer: C Let $X = 3$ and $Y = 7$. $1^{\text{st}}$ pass: $X = 3, Y = 4$ $2^{\text{nd}}$ pass: $X = 3, Y = 1$ $3^{\text{rd}}$ pass: $X = 2, Y = 1$ $4^{\text{th}}$ pass: $X = 1, Y = 1$ write $(X)$, which writes $1$. Ref: https://en.wikipedia.org/wiki/Euclidean_algorithm Rajarshi Sarkar answered Jun 2, 2015 • edited Apr 25, 2021 by Lakshman Bhaiya Rajarshi Sarkar comment Share Follow See all 0 reply Please log in or register to add a comment.
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 Rajesh Pradhan answered Nov 15, 2016 Rajesh Pradhan comment Share Follow See all 0 reply Please log in or register to add a comment.
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. Radha mohan answered Nov 29, 2018 Radha mohan comment Share Follow See all 0 reply Please log in or register to add a comment.