enter 發表於 2015-5-8 17:51:10

求二數的最大公因數

#include <stdio.h>

int main(void){

int x,y,i,j,z,gcd; // gcd就是 greatest common divisor 最大公因數

printf("enter 2 numbers:\n");
scanf("%d %d",&x,&y);

if(x>y)
{i=x;
j=y;}
if(x<y)
{i=y;
j=x;}

for(z=1;z<=j;z++)
{
        if((i%z==0)&&(j%z==0)) gcd=z;
       
}
if(gcd==1){
        printf("There is no gcd of %d and %d.",x,y);
}else {
printf("The gcd of %d and %d is %d.",x,y,gcd);
}

return 0;
}
頁: [1]
查看完整版本: 求二數的最大公因數