求最小公倍数和最大公约数

程序填空,不要改变与输入输出有关的语句。
输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入两个正整数m和n,输出它们的最小公倍数和最大公约数。
输入输出示例:括号内为说明
输入:
3        (repeat=3)
3 7      (m=3,n=7)
24 4     (m=24,n=4)
24 18    (m=24,n=18)
输出:
21 is the least common multiple of 3 and 7, 1 is the greatest common divisor of 3 and 7.
24 is the least common multiple of 24 and 4, 4 is the greatest common divisor of 24 and 4.
72 is the least common multiple of 24 and 18, 6 is the greatest common divisor of 24 and 18.
#include <stdio.h>
int main(void)
{
    int gcd, lcm, m, n;  
    int repeat, ri;  

    scanf("%d", &repeat);
    for(ri = 1; ri <= repeat; ri++){
        scanf("%d", &m);
        scanf("%d", &n);
        if(m <= 0 || n <= 0)
            printf("m <= 0 or n <= 0");
        else{
        	int i, h, k;
        	i = m; h = n;
			if(m > n){
				while (m%n != 0)
				{   
					k = n;
					n = m % n;
					m = k;
				}
				gcd = n;
		    }
		    if(n > m){
				while (n%m != 0)
				{   
					k = m;
					m = n % m;
					n = k;
				}
				gcd = m;
		    }
		    m = i; n = h;
			lcm = m*n / gcd;
			
            printf("%d is the least common multiple of %d and %d, %d is the greatest common divisor of %d and %d.\n", lcm, m, n, gcd, m, n);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值