Title: Enter two positive integers m and n, and ask for their gcd and LCM.
In c language code as follows:
#include <stdio.h>
void Main ()
{
int p, r, N, M, temp;
printf ("Please enter two positive integers:");
scanf ("%d,%d", &n, &m);
if (n < m)
{
temp = n;
n = m;
m = temp;
}
p = n*m;
while (M!= 0)
{
r = n%m;
n = m;
m = r;
}
printf ("Their gcd is:%d\n", n);
printf ("Their LCM is:%d\n", n);
}
This uses the Euclidean method, the principle of the Euclidean method is as follows:
The gcd of two integers is the largest positive integer that can divide them at the same time. The Euclidean method is based on the principle that the gcd of two integers equals the gcd of the smaller number and the difference of two numbers. For example, the GCD of 252 and 105 is 252 = 21x12;105 = 21x5), because 252−105 = 147, so the GCD of 147 and 105 is 21. In this process, the larger number shrinks, so the same calculation continues to shrink the two numbers until one of them becomes 0. At this point, the remaining not to zero is the number of two gcd.
Note: LCM equals two number of product divided by minimum Convention number