练习 03-02-06
使用模块化程序设计思路,用辗转相除法求两个数的Greatest Common Divisor.
please enter two integers:
40 8
The greatest common divisor is:8
--------------------------------
Process exited after 6.299 seconds with return value 0
请按任意键继续. . .
代码如下:
//greatest common divisor(gcd)
#include<stdio.h>
int main()
{
int gcd(int x,int y);
int a,b;
printf("please enter two integers:\n");
scanf("%d%d",&a,&b);
int result;
result=gcd(a,b);
printf("The greatest common divisor is:%d",result);
return 0;
}
int gcd(int x,int y)
{
int z;
do{
z=x%y;
x=y;
y=z;
}while(y!=0);
return x;
}