最大公约数与最小公倍数C++实现
最大公约数为辗转相除法求得, 最小公倍数为两数之积与最大公约数的比值
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 / mcm(a, b); 29 }