java 找出用户输入的两个最小公倍数
时间: 2024-01-23 20:43:24 浏览: 31
以下是一种解决方案:
```java
import java.util.Scanner;
public class LCM {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入两个正整数:");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int lcm = getLCM(num1, num2);
System.out.println(num1 + "和" + num2 + "的最小公倍数是:" + lcm);
}
// 辗转相除法求最大公约数
private static int getGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return getGCD(b, a % b);
}
}
// 最小公倍数 = 两数之积 / 最大公约数
private static int getLCM(int a, int b) {
return a * b / getGCD(a, b);
}
}
```
运行结果:
```
请输入两个正整数:12 18
12和18的最小公倍数是:36
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)