public class GCD_and_LCM {
public static void main(String args[]){
System.out.println("请输入两个整数:");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
GCD_and_LCM gl=new GCD_and_LCM();
System.out.println(a+"和"+b+"的最大公约数和最小公倍数分别为:");
System.out.println(gl.gcd(a,b));
System.out.println(gl.lcm(a, b));
}
public int gcd(int a,int b){
if(b==0){
return a;
}
else{
while(b!=0){
int r=a%b;
a=b;
b=r;
}
return gcd(a,b);
}
}
public int lcm(int a,int b){
return a*b/gcd(a,b);
}
}
原文:https://www.cnblogs.com/MuXiao-DJT/p/8783357.html