当前位置:K88软件开发文章中心编程语言C/C++C/C++01 → 文章内容

求两个正整数m和n的最大公约数和最小公倍数

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:07:14

:2011-03-20 12:18:35

C语言练习题

从键盘输入两个数m和n,求其最大公约数和最小公倍数。

实现方法如下:

main()
{
    int m,n,a,b;
    int p,r,temp;
    printf("Please input 2 integer(m,n):");
    scanf("%d,%d",&m,&n);
    a = m; b = n;
    if(b<a)
    {
        temp = a;
        a = b;
        b = temp;
    }
    p = a * b;
    while(a != 0)
    {
        r = b % a;
        b = a;
        a = r;
    }

    printf("The Greatest Common Divisor %d and %d is: %d\n",m,n,b);
    printf("The Least Common Multiple %d and %d is: %d\n",m,n,p/b);
}

运行结果为:


求两个正整数m和n的最大公约数和最小公倍数