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); } |
chevron_right
filter_none
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
Recommended Posts:
- Algorithms | Divide and Conquer | Question 6
- Algorithms | Divide and Conquer | Question 3
- Algorithms | Divide and Conquer | Question 6
- Algorithms | Divide and Conquer | Question 5
- Algorithms | Divide and Conquer | Question 4
- Algorithms Quiz | Greedy Algorithms | Question 8
- Algorithms | Analysis of Algorithms (Recurrences) | Question 8
- Algorithms | Analysis of Algorithms (Recurrences) | Question 6
- Algorithms | Analysis of Algorithms (Recurrences) | Question 7
- Algorithms | Analysis of Algorithms (Recurrences) | Question 1
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 3
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 2
- Algorithms | Analysis of Algorithms (Recurrences) | Question 11
- Algorithms | Analysis of Algorithms (Recurrences) | Question 9
- Algorithms | Analysis of Algorithms (Recurrences) | Question 4
- Algorithms | Analysis of Algorithms | Question 4
- Algorithms Quiz | Bit Algorithms | Question 4
- Algorithms | Analysis of Algorithms | Question 9

