C++实现输入两个正整数m和n,求其最大公约数和最小公倍数
2个回答
展开全部
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "输入两个数:" << endl;
cin >> a >> b;
int temp, num1, num2;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
num1 = a;
num2 = b;
while(num2 != 0)
{
temp = num1 % num2;
num1 = num2;
num2 = temp;
}
cout << "最大公约数为:" << num1
<< " 最小公倍数为:" << (a * b) / num1 << endl;
return 0;
}
using namespace std;
int main()
{
int a, b;
cout << "输入两个数:" << endl;
cin >> a >> b;
int temp, num1, num2;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
num1 = a;
num2 = b;
while(num2 != 0)
{
temp = num1 % num2;
num1 = num2;
num2 = temp;
}
cout << "最大公约数为:" << num1
<< " 最小公倍数为:" << (a * b) / num1 << endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询