编写一段vc++的代码,求两个数的最大公约数和最小公倍数
时间: 2023-11-17 13:09:31 浏览: 41
以下是求两个数的最大公约数和最小公倍数的VC++代码:
```c++
#include <iostream>
using namespace std;
int gcd(int a, int b) { // 求最大公约数
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b) { // 求最小公倍数
return a * b / gcd(a, b);
}
int main() {
int a, b;
cout << "请输入两个整数: ";
cin >> a >> b;
cout << "它们的最大公约数是: " << gcd(a, b) << endl;
cout << "它们的最小公倍数是: " << lcm(a, b) << endl;
return 0;
}
```
该程序先定义了两个函数`gcd`和`lcm`,分别用来求最大公约数和最小公倍数。在`main`函数中,用户输入两个整数,然后调用`gcd`和`lcm`函数分别求出它们的最大公约数和最小公倍数并输出。
相关问题
编写一段vc6.0的代码,求两个数的最大公约数和最小公倍数
以下是求两个数的最大公约数和最小公倍数的VC6.0代码:
```c++
#include <iostream>
using namespace std;
// 求两个数的最大公约数
int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b, a % b);
}
// 求两个数的最小公倍数
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
int main()
{
int num1, num2;
cout << "请输入两个整数:" << endl;
cin >> num1 >> num2;
cout << "最大公约数为:" << gcd(num1, num2) << endl;
cout << "最小公倍数为:" << lcm(num1, num2) << endl;
return 0;
}
```
注意:VC6.0不支持 `using namespace std`,需要将命名空间 std:: 显式写出。
编写一个函数求两个数的最大公约数和最小公倍数
以下是Python编写一个函数求两个数的最大公约数和最小公倍数的例子:
1. 求最大公约数的函数:
```python
def gcd(a, b):
while b:
a, b = b, a % b
return a
```
2. 求最小公倍数的函数:
```python
def lcm(a, b):
return a * b // gcd(a, b)
```
这两个函数分别用于求两个数的最大公约数和最小公倍数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)