使用辗转相除法求GCD与LCM
import java.util.Scanner;
public class 求两整数的GCD与LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入两个整数:");
int m = sc.nextInt();
int n = sc.nextInt();
int x = m, y = n, temp;
if (x < y) {//将大的数作被除数
temp = x;
x = y;
y = temp;
}
//使用辗转相除法求出最大公约数
while (x % y != 0) {
temp = x % y;
x = y;
y = temp;
//当其余数为0时,最大公约数为此时的除数
}
System.out.println("最大公约数gcd为:"+ y);
System.out.println("最小公倍数lcm为:"+ m * n / y);
}
}