用C语言编程定义函数,形参为两个int类型变量,功能是显示这两个数的最大公约数和最小公倍数。
时间: 2023-06-01 08:05:26 浏览: 128
好的,这个问题我可以回答。以下是代码示例:
```
#include <stdio.h>
int gcd(int a, int b);
int lcm(int a, int b);
int main() {
int num1, num2;
printf("请输入两个整数:\n");
scanf("%d%d", &num1, &num2);
printf("最大公约数为 %d\n", gcd(num1, num2));
printf("最小公倍数为 %d\n", lcm(num1, num2));
return 0;
}
// 计算最大公约数
int gcd(int a, int b) {
int remainder;
while (b != 0) {
remainder = a % b;
a = b;
b = remainder;
}
return a;
}
// 计算最小公倍数
int lcm(int a, int b) {
int multiple = a > b ? a : b;
while (1) {
if (multiple % a == 0 && multiple % b == 0) {
break;
}
multiple++;
}
return multiple;
}
```
这个程序定义了两个函数 `gcd` 和 `lcm`,分别用于求最大公约数和最小公倍数。主函数中先输入两个整数,然后调用这两个函数并输出结果。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)