public class Test4 {
/*
计算两个数的最大公约数和最小公倍数。
*/
public static void main(String[] args) {
int num1 = 30,num2 = 45,gcd = 1;
int lcm =(num1*num2) / gcd(num1,num2);
System.out.println("GCD of" + num1+ "and" +num2+ ":"+gcd(num1,num2));
System.out.println("LCM of"+num1+"and"+num2+":"+lcm);
}
public static int gcd(int a,int b){
int div = 1;
for(int i= 1;i<=a && i<=b;i++){
if(a % i == 0 && b % i == 0){
div = i;
}
}
return div;
}
}
java编程题4
最新推荐文章于 2024-11-09 12:43:52 发布