java applet (six)

[6] Program
Title: Enter two positive integers m and n, and seeking the least common multiple of the greatest common divisor.
In the cycle, as long as the divisor is not equal to 0, the smaller number with a larger number by the smaller number as a next cycle of large numbers,
the remainder obtained as the next cycle of the smaller number, so loop until the smaller number is 0,
return a large number, this number is the greatest common divisor of the least common multiple of the product divided by the greatest common divisor of two numbers.

package case50;

import java.util.Scanner;

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

//计算最大公约数
class CalGCD {

    public int CalGCD(int x, int y) {

        int t = 0;
        if (x > y) {
            t = x;
            x = y;
            y = t;
        }
        while (x != 0) {
            if (x == y)
                return y;
            else {
                int k = y % x;
                y = x;
                x = k;
            }
        }
        return y;
    }

}

public class Case06 {

    public static void main(String[] args) {

        int num1 = 0, num2 = 0, gcd = 0, lcm = 0;// gcd 最大公约数 lcm 最小公倍数
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个数字");
        num1 = input.nextInt();
        System.out.println("请再输入一个数字");
        num2 = input.nextInt();
        input.close();
        CalGCD GCD = new CalGCD();
        gcd = GCD.CalGCD(num1, num2);
        lcm = num1 * num2 / gcd;
        System.out.println("最大公约数:" + gcd + " 最小公倍数: " + lcm);

    }

}

Guess you like

Origin www.cnblogs.com/YanJieMao/p/12363997.html