0
点赞
收藏
分享

微信扫一扫

根据最大公约数的如下3条性质,采用递归法编写计算最大公约数的函数Gcd(),在主函数中调用该函数计算并输出从键盘任意输入的两正整数的最大公约数。性质1 如果a>b,则a和b与a-b和b的最大公约数

pipu 2022-04-23 阅读 66
c语言

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

举报

相关推荐

0 条评论