输入m,n求最大公约数和最小公倍数c语言
时间: 2023-12-03 15:39:29 浏览: 128
C语言求最大公约数和最小公倍数
以下是求最大公约数和最小公倍数的C语言代码:
```c
#include <stdio.h>
int main()
{
int m, n, i, gcd, lcm;
printf("Enter two positive integers: ");
scanf("%d %d", &m, &n);
// 求最大公约数
for(i=1; i<=m && i<=n; ++i)
{
if(m%i==0 && n%i==0)
gcd = i;
}
// 求最小公倍数
lcm = m*n/gcd;
printf("Greatest common divisor of %d and %d is %d\n", m, n, gcd);
printf("Least common multiple of %d and %d is %d\n", m, n, lcm);
return 0;
}
```
运行结果:
```
Enter two positive integers: 12 30
Greatest common divisor of 12 and 30 is 6
Least common multiple of 12 and 30 is 60
```
阅读全文