求最大公约数和最小公倍数
public class first {
public static void main(String []args){
int a = 12;
int b = 18;
while(b!=0){
int temp = a%b;
a = b;
b = temp;
}
int num = a;
System.out.println(num);
System.out.println(12*18/num);
}
}
本文介绍如何使用Java编写一个简单的程序来求两个整数12和18的最大公约数(GCD),并通过求余运算逐步找到,最后计算并输出它们的最小公倍数(LCM)。
求最大公约数和最小公倍数
public class first {
public static void main(String []args){
int a = 12;
int b = 18;
while(b!=0){
int temp = a%b;
a = b;
b = temp;
}
int num = a;
System.out.println(num);
System.out.println(12*18/num);
}
}
2410

被折叠的 条评论
为什么被折叠?