Assume X and Y are non-zero positive integers. Consider the following…

2017

Assume X and Y are non-zero positive integers. Consider the following pseudo-code fragment: while X <> Y do if X > Y then X ← X − Y else Y ← Y − X endif end while print (X) What is the code doing?

Answer: A. It computes the GCD of two numbers.The algorithm repeatedly subtracts the smaller number from the larger until X = Y, then prints X. This is the subtraction-based form of Euclid’s algorithm and…

  1. A.

    It computes the GCD of two numbers.

  2. B.

    It computes the LCM of two numbers.

  3. C.

    It finds the smallest of two numbers.

  4. D.

    It divides the largest number by the smaller.

Attempted by 17 students.

Show answer & explanation

Correct answer: A

The algorithm repeatedly subtracts the smaller number from the larger until X = Y, then prints X. This is the subtraction-based form of Euclid’s algorithm and returns the greatest common divisor (GCD) of the original inputs. यह एल्गोरिथ्म छोटी संख्या को बड़ी संख्या से बार‑बार घटाकर तब तक चलता है जब तक X = Y न हो जाए, और फिर X प्रिंट करता है। यह यूकलिड के घटाव‑आधारित विधि का रूप है और मूल इनपुट्स का महत्तम समापवर्तक (GCD) देता है। Key invariant: gcd(X, Y) does not change during a subtraction step because gcd(a - b, b) = gcd(a, b).

Explore the full course: Accenture Preparation

Loading lesson…