求最大公约数和最小公倍数

1.最大公约数辗转相除法

int t;
    while(b!=0){
        t=a%b;
        a=b;
        b=t;

    }

    printf("the gcd is %d\n",a);

2.最小公倍数
最小公倍数乘以最大公约数等于两数乘积,所以最小公倍数等于两数乘积除以最大公约数。

    
#include<stdio.h>
#include<math.h>
int main(void){

    printf("please input two number:");
    int a=0,b=0;
    scanf("%d %d",&a,&b);
    int  temp1=a,temp2=b;
    int t;
    while(b!=0){
        t=a%b;
        a=b;
        b=t;

    }

    printf("the gcd is %d\n",a);

    int zxg=temp1*temp2/a;
    printf("the zxgbs is %d\n",zxg);



}
posted @ 2024-04-23 10:58  zhongta  阅读(37)  评论(0)    收藏  举报