c语言循环结构用辗转相除法求出两个正整数的最大公约数和最小公倍数
时间: 2023-11-01 09:06:44 浏览: 136
辗转相除法,求最大公约数和最小公倍数
5星 · 资源好评率100%
最大公约数:
```c
#include <stdio.h>
int main() {
int num1, num2, remainder, gcd;
printf("请输入两个正整数:\n");
scanf("%d %d", &num1, &num2);
while(num2 != 0) {
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
gcd = num1;
printf("最大公约数为:%d\n", gcd);
return 0;
}
```
最小公倍数:
```c
#include <stdio.h>
int main() {
int num1, num2, remainder, gcd, lcm;
printf("请输入两个正整数:\n");
scanf("%d %d", &num1, &num2);
int temp1 = num1, temp2 = num2;
while(num2 != 0) {
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
gcd = num1;
lcm = temp1 * temp2 / gcd;
printf("最小公倍数为:%d\n", lcm);
return 0;
}
```
阅读全文