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

时间:2022-02-27 12:42:25

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);
}
}