java 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。
作者:互联网
import java.util.Scanner;
public class Pro9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int a;
int b;
int r;
//最大公约数初值
int gcd = 1;
//最小公倍数
int lcm = 0;
System.out.println("请输入一个整数:");
a = in.nextInt();
System.out.println("请再输入一个整数:");
b = in.nextInt();
int oa = a;
int ob = b;
//辗转相除法
while (b != 0) {
r = a%b;
a = b;
b = r;
}
gcd = a;
lcm = oa*ob/gcd;
System.out.println( oa+ "和"+ ob +"的最大公约数为:"+ gcd+"!");
System.out.println( oa+ "和"+ ob +"的最小公倍数为:"+ lcm+"!");
}
}
标签:java,gcd,公倍数,System,oa,ob,int,最大公约数,out 来源: https://www.cnblogs.com/rainbow7colours/p/16339564.html