【程序6】 求最大公约数和最小公倍数

题目:输入两个正整数m和n,求其最大公约数和最小公倍数。   
/**在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /

public class lianxi06
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        int a, b, m;
        Scanner s = new Scanner(System.in);
        System.out.print("键入一个整数: ");
        a = s.nextInt();
        System.out.print("再键入一个整数: ");
        b = s.nextInt();
        deff cd = new deff();
        m = cd.deff(a, b);
        int n = a * b / m;
        System.out.println("最大公约数: " + m);
        System.out.println("最小公倍数: " + n);
    }

}

class deff
{
    public int deff(int x, int y)
    {
        int t;
        if (x < y)
        {
            t = x;
            x = y;
            y = t;
        }
        while (y != 0)
        {
            if (x == y)
                return x;
            else
            {
                int k = x % y;
                x = y;
                y = k;
            }
        }
        return x;
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值