题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

public class Gong {public static void main(String[] args) {@SuppressWarnings("resource")Scanner scanner = new Scanner(System.in);System.out.println("请输入两个正整数(12 2):");int m = scanner.nextInt();int n = scanner.nextInt();int divisor = getGreatestCommonDivisor(m, n);System.out.println("最大公约数:" + divisor);System.out.println("最小公倍数:" + m / divisor * n);}static int getGreatestCommonDivisor(int m, int n) {int r = m % n; //两数取余while (r != 0) { //如果余数不为零,n对上一个余数取余,直到为余数为0为止m = n;n = r;r = m % n;}return n;//返回最大公约数}}

你可能感兴趣的:(java)