在JAVA中怎么实现求最大公约数和最小公倍数

如题所述

import java.util.Scanner;

public class Gongbei {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
int x = sc.nextInt();
System.out.println("输入第二个数:");
int y = sc.nextInt();
System.out.println("最大公约数:"+gongyue(x,y));
System.out.println("最小公倍数:"+gongbei(x,y));


}
public static int gongyue(int x,int y){//最大公约数
if(x>y){
int t = x;
x = y;
y = t;
}
while(x!=0){
int temp = y%x;
y = x;
x = temp;
}
return y;
}
public static int gongbei(int x,int y){//最小公倍数
int a = x,b = y;
int g = gongyue(a,b);
return x*y/g;
}

}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答