Using a whileloop determine the greatest common divisor GCD

Using a while-loop, determine the greatest common divisor (GCD) of two numbers, m and n. GCD = n when mod(m, n) = 0. If mod(m, n) ~=0 then reassign values to m and n as follows and: temp = m m = n n = mod(temp, n) The above equations use a temp variable in order to preserve the original value of m, which is needed to recalculate and reassign the variable n.

Solution

Answer :


#include<stdio.h>
int main(){
int m,n,gcd;
printf(\"\ Enter two numbers: \");
scanf(\"%d %d\",&m,&n);
gcd=gcdvalue(m,n);
printf(\"\ GCD of %d and %d is: %d\",m,n,gcd);
return 0;
}

int gcdvalue(int x,int y){
while(x!=y){
if(x>y)
return gcdvalue(x-y,y);
else
return gcdvalue(x,y-x);
}
return x;
}

 Using a while-loop, determine the greatest common divisor (GCD) of two numbers, m and n. GCD = n when mod(m, n) = 0. If mod(m, n) ~=0 then reassign values to m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site