输入两个正整数m和n,求其最大公约数和最小公倍数。java程序设计
时间: 2024-03-24 13:32:40 浏览: 85
以下是Java程序设计求两个正整数最大公约数和最小公倍数的代码示例:
```java
public class GcdLcm {
private int a;
private int b;
public GcdLcm(int a, int b) {
this.a = a;
this.b = b;
}
// 求最大公约数
public int gcd() {
int temp;
if (a < b) {
temp = a;
a = b;
b = temp;
}
while (b != 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
// 求最小公倍数
public int lcm() {
return a * b / gcd();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入两个正整数:");
int m = scanner.nextInt();
int n = scanner.nextInt();
GcdLcm gcdLcm = new GcdLcm(m, n);
System.out.println("最大公约数为:" + gcdLcm.gcd());
System.out.println("最小公倍数为:" + gcdLcm.lcm());
}
}
```
阅读全文