用简单的方式java输入两个正整数m和n,求其最大公约数和最小公倍数
时间: 2024-03-24 07:32:22 浏览: 28
以下是Java求两个正整数m和n的最大公约数和最小公倍数的代码示例:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入第一个正整数m:");
int m = input.nextInt();
System.out.print("请输入第二个正整数n:");
int n = input.nextInt();
int gcd = getGcd(m, n);
int lcm = getLcm(m, n, gcd);
System.out.println("最大公约数为:" + gcd);
System.out.println("最小公倍数为:" + lcm);
}
// 求最大公约数
public static int getGcd(int m, int n) {
if (n == 0) {
return m;
} else {
return getGcd(n, m % n);
}
}
// 求最小公倍数
public static int getLcm(int m, int n, int gcd) {
return m * n / gcd;
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)