code.club

標題: 求二數的最大公因數 [打印本頁]

作者: enter    時間: 2015-5-8 17:51
標題: 求二數的最大公因數
#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;
}





歡迎光臨 code.club (http://code.club/) Powered by Discuz! X3.2