求三个整数的最小公倍数的循环C语言代码
时间: 2023-10-26 19:07:15 浏览: 51
可以使用以下代码利用循环求三个整数的最小公倍数:
```c
#include <stdio.h>
// 求两个数的最大公约数
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
// 求三个数的最小公倍数
int lcm(int a, int b, int c) {
int max = a > b ? a : b; // 找出三个数中的最大值
max = max > c ? max : c;
int result = max; // 初始化结果为最大值
while (1) { // 无限循环
if (result % a == 0 && result % b == 0 && result % c == 0) {
break; // 如果找到了最小公倍数,跳出循环
}
result++; // 否则继续增加结果,直到找到最小公倍数
}
return result;
}
int main() {
int a, b, c;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
int result = lcm(a, b, c);
printf("它们的最小公倍数是:%d\n", result);
return 0;
}
```
这个程序中,`lcm()` 函数中使用了一个无限循环,每次增加结果的值,直到找到三个数的最小公倍数为止。在每次循环中,判断当前的结果是否同时能够被三个数整除,如果能够整除,则说明找到了最小公倍数,跳出循环并返回结果。