C语言:计算两个正整数的最大公约数
最大公约数的函数原型:int MaxCommonFactor( int a, int b);
返回值:返回的是最大公约数;若输入的数据有任意一个不满足条件,返回值是-1
。
#include<stdio.h>
int MaxCommonFactor( int a, int b)
{
int c;
if(a<=0||b<=0)
return -1;
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int main(void)
{
int a,b,c;
scanf("%d,%d",&a,&b);
c=MaxCommonFactor(a,b);
printf("%d\n",c);
return 0;
}