wiz-icon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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

A
x mod y using repeated subtraction
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
the greatest common divisor of x and y
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
C
the least common multiple of x and y
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
x÷y, using repeated subtraction
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is B the greatest common divisor of x and y
The given C program is non-recursive. So we will get the value of n after some iteration. The main thing about the program is that.
  1. if m = n then n is the GCD
  2. if m > n then m = m- n
  3. if m < n then n = n- m
For example let x= 40 and y = 32
m = x = 40 and n = y = 32
m > n so apply the 2nd condition
m = m - n
m = 40 - 32 = 8
m = 8 and n = 8
apply the first condition
GCD (40, 32) = 8

flag
Suggest Corrections
thumbs-up
0
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
The Tripartite Struggle
HISTORY
Watch in App
Join BYJU'S Learning Program
CrossIcon