Current location - Education and Training Encyclopedia - Graduation thesis - N methods for finding the greatest common factor and the least common multiple in C language?
N methods for finding the greatest common factor and the least common multiple in C language?
(1)

# include & ltstdio.h & gt

int f(int a,int b,int n)

{if(a%n+b%n) n=f(a,b,n- 1);

Returns n;

}

void main()

{int a,b,t,n,m;

Printf ("enter a, b: \ n");

Scanf("%d%d ",&i, & b);

n =(a & gt; b)? B: a;

m=a*b/f(a,b,n);

Printf ("Gong Yue number of big mouth: %d\nNumber of small mouth:% d \ n", f(a, b, n), m);

}

(2)

Replace the recursion above with a loop.

# include & ltstdio.h & gt

void main()

{

int x,y,a,m,n,t;

Printf ("Please enter two numbers x, y \ n");

scanf("%d%d ",& ampx & amp; y);

for(a = x & gt; y? x:y; a & gt0; Answer-)

if(x % a = = 0 & amp; & ampy%a==0)

{

Printf("x, y greatest common divisor %d\n ",a);

Break;

}

m = x/a;

n = y/a;

t = a * m * n;

Printf("x, y least common multiple %d\n ",t);

}

(3)

This is the best way, so we must master it.

/view/255668.html? wtp=tt

With the big convention, twice divided by the big convention is the small common multiple.