Consider the following C program main() { int x, y,

Consider the following C program

main()
{
  int x, y, m, n;
  scanf ("%d %d", &x, &y);
  /* x > 0 and y > 0 */
  m = x; n = y;
  while (m != n)
  {
    if(m>n)
      m = m - n;
    else
      n = n - m;
  }
  printf("%d", n);
}

The program computes (GATE CS 2004)...

  1. x + y using repeated subtraction

  2. x mod y using repeated subtraction

  3. the greatest common divisor of x and y

  4. the least common multiple of x and y

Answer

Answer: 3. the greatest common divisor of x and y

It's an implementation of Euclid's GCD algorithm.

1Comment
Shubh @shubhammeshr
19 Aug 2017 11:40 am

why option 4 is wrong??

Pages