Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Algorithms | Divide and Conquer | Question 2

  • Last Updated : 09 Feb, 2013

Consider the following C program




int 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); 
}

What does the program compute? (GATE CS 2004)
(A) x + y using repeated subtraction
(B) x mod y using repeated subtraction
(C) the greatest common divisor of x and y
(D) the least common multiple of x and y


Answer: (C)

Explanation: This is an implementation of Euclid’s algorithm to find GCD

My Personal Notes arrow_drop_up
Recommended Articles
Page :