使用递归求最大公约数

#include <stdio.h>
int Gcd(int a, int b)
{
    if(a==b) return a;
    if(a>b) return Gcd(a-b,b);
    if(a<b) return Gcd(a,b-a);
}
int main()
{
    printf("Input a,b:");
    int a,b;
    scanf("%d,%d",&a,&b);
    if(a<=0||b<=0) {
        printf("Input number should be positive!\n");
        return 0;
    } else {
        printf("Greatest Common Divisor of %d and %d is %d\n",a,b,Gcd(a,b));
    }
    return 0;
}

 

posted @ 2014-04-01 07:06  xzenith  阅读(445)  评论(0编辑  收藏  举报