辗转相除法求最大公因数

  • 它的具体做法是:用较小数除较大数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。
#include <stdio.h>
#include <stdlib.h>
int fun(int a,int b){
int c,r = a%b;//不用考虑这个大小,先后关系 ,自己调试就行
while(r!=0){
a = b;
b = r;
r = a%b;
}
return b;
}
int main() {
int a,b;
scanf("%d%d",&a,&b);
printf("最大公因数是:%d",fun(a,b));
//最小公倍数:只要用这两个数的乘积除以最大公因数即可
// printf("最小公倍数是:%d",(a*b)/fun(a,b));
return 0;
}