写两个函数,分别求两个整数的最大公约数和最小公倍数

来源:互联网 发布:c语言打印杨辉三角8行 编辑:程序博客网 时间:2024/05/17 03:17
写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int gcd(intm,int n);
    int lcm(inta,int b,int igcd);
    inta,b,igcd,ilcm;
   printf("enter rwo number:\n");
   scanf("%d,%d",&a,&b);

   if(a==0)
    {
       igcd = b;
       ilcm = 0;
    }
    elseif(b==0)
    {
       igcd = a;
       ilcm = 0;
    }
    else
    {
       igcd = gcd(a,b);
       ilcm = lcm(a,b,igcd);
    }

    printf("%dand %d's  greatest common divisor is%d\n",a,b,igcd);
    printf("%dand %d's  least common multiple is%d\n",a,b,ilcm);
    return0;
}


int gcd(int m,int n)//greatest common divisor
{
    inttemp;
   if(n>m)
    {
       temp = n;
       n = m;
       m = temp;
    }
    while((temp= m%n)!=0)
    {
       m = n;
       n = temp;
    }

    returnn;

}

int lcm(int a,int b,int igcd)//]least common multiple
{
    return(a*b)/igcd;
}


my codes seem not concise as others.But i consider thecondition of when one of the number is zero in which itsamount of calculation is least and it keeps two number asthey used to be.
0 0
原创粉丝点击