求最大公约数和最小公倍数的C++代码

C++代码如下所示:

#include<iostream>
using namespace std;
int main()
{
    
    
	int n1, n2,LCM;
	cout << "输入两个整数: ";
	cin >> n1 >> n2;
	LCM = n1*n2;
	while (n1 != n2)
	{
    
    
		if (n1 > n2)
			n1 -= n2;
		else
			n2 -= n1;
	}

	cout << "HCF = " << n1 << endl;;
	cout << "LCM= " << LCM / n1 << endl;
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36294338/article/details/108704438