//最大公约数
package com.imooc.demo;
/**
* 求两个数的最大公约数
* 辗转相除法
*/
import java.util.Scanner;
public class GcdDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a:");
int a = scanner.nextInt();
System.out.println("请输入b:");
int b = scanner.nextInt();
System.out.println("最大公约数:" + new GcdDemo().getGcd(a,b));
}
public int getGcd(int a ,int b ){
while (b>0) {
int temp = b;
b = a%b;
a = temp;
}
return a ;
}
}package com.imooc.demo;
/**
* 求两个正整数的的最小公倍数
* 两个数的最小公倍数等于其乘积除以最大公约数
*/
import java.util.Scanner;
public class GcmDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入a:");
int a = scanner.nextInt();
System.out.println("请输入b:");
int b = scanner.nextInt();
System.out.println("最小公倍数:" + new GcmDemo().getGcm(a,b));
}
private int getGcm(int a, int b){
return a*b/(new GcdDemo().getGcd(a, b));//两个数的最小公倍数等于其乘积除以最大公约数
}
}