[java]代码库
package homework;
import java.util.Scanner;
public class 最大公约数和最大公倍数 {
public static int GCD(int a,int b){
int tmp=1;
for(int i=0;;i++){
tmp=a%b;
if(tmp==0){
break;
}
a=b;
b=tmp;
}
return b;
}
public static int LCM(int a,int b){
int temp=GCD(a,b);
return (a*b)/temp;
}
public static void main(String[] args) {
int m,n;
Scanner reader=new Scanner(System.in);
System.out.println("enter m and n");
m=reader.nextInt();
n=reader.nextInt();
int gcd,lcm; //gcd 最大公约数,LCM最小公倍数
gcd=GCD(m,n);
lcm=LCM(m,n);
System.out.print("最大公因数:");
System.out.println(gcd);
System.out.print("最小公倍数:");
System.out.println(lcm);
}
}
[代码运行效果截图]