To achieve the greatest common divisor and least common multiple C ++

The greatest common divisor is calculated Euclidean algorithm, the least common multiple is a product of the ratio of the greatest common divisor of two numbers

 1 #include<iostream>
 2 using namespace std;
 3 int gcd(int, int);
 4 int mcm(int, int);
 5 int main()
 6 {
 7     int a, b;
 8     cout << "enter a and b: " << endl;
 9     cin >> a >> b;
10     cout << "gcd : " << gcd(a, b) << endl;
11     cout <<"mcm : " << mcm(a, b) << endl;
12 
13     system("pause");
14     return 0;
15 }
16 int gcd(int a, int b)
17 {
18     while (a%b != 0) 
19     {
20         int tmp = a;
21         a = b;
22         b = tmp % a;
23     }
24     return b;
25 }
26 int mcm(int a, int b)
27 {
28     return a * b / gys(a, b);
29 }

 

Guess you like

Origin www.cnblogs.com/Aurora-Borealis/p/summer710-1.html