Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C program :
    main ()
       {int x, y, m, n;
       scanf (“%d %d” , &x, &y);
       / * Assume 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
    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
Correct Option: C

This is an implementation of Euclid's algorithm to find GCD
Here if m > n, then m = m – n
m < n, then n = n – m
Let take X = 24, Y = 9
Then m = 24, n = 9

iteration m n
124 – 9 = 15 9
215 – 9 = 6 9
36 9 – 6 = 3
46 – 3 = 3 3

Here m = n, so n returned
Which is GCD (Greatest common divisor) of X&Y
Hence (c) is correct option.



Your comments will be displayed only after manual approval.