任意输入两个数,求其最大公约数和最小公倍数

时间:2020-12-28 00:36:45

import java.util.Scanner;


public class Practice {
public static void main(String[] args) {
int a = 0, b = 0, sum = 0;
System.out.println("请输入2个整数:");
Scanner scan = new Scanner(System.in);
a = scan.nextInt();
//Scanner sc = new Scanner(System.in);
b = scan.nextInt();
// System.out.println("a=" + a);
// System.out.println("b=" + b);
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 && b % i == 0) {
sum = i;
}
}
System.out.println(a + "," + b + "的最大公约数为:" + sum);
System.out.println(a + "," + b + "的最小公倍数为:" + a * b / sum);
}
}