题目:
随机输出两个数 m,n,求最大公约数、和最大公倍数
代码:
package scan;
import java.util.Scanner;
public class Yy{
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入正数m:");
int m = scan.nextInt();
System.out.println("请输入正数n:");
int n = scan.nextInt();
int min = (m < n) ? m : n ;
for(int i = min ; i >= 1 ; i--) {
if(m % i == 0 && n % i == 0) {
System.out.println("最大公约数为:" + i);
break;
}
}
int max = (m > n) ? m : n ;
for(int i = max ; i <= m * n ; i++) {
if(i % m == 0 && i % n == 0) {
System.out.println("最大公倍数为:" + i);
break;
}
}
}
}