java 求最大公约数和最小公倍数

package com.oop;

public class Demo2 {
	//使用欧几里得算法求解数m和数n最大公约数
    public int getGcd(int m,int n){
        while(n > 0){
            int temp = m % n;
            m = n;
            n = temp;
        }
        return m;
    }
    
    //求解数m和n和最小公倍数
    public int getLcm(int m,int n){
        int gcd = getGcd(m,n);
        int result = m*n / gcd;
        return result;
    }
    
    public static void main(String[] args){
    	Demo2 test = new Demo2();
        System.out.println("60和12的最大公约数:"+test.getGcd(60, 12));
        System.out.println("60和12的最小公倍数:"+test.getLcm(60, 12));
    }
}
posted @ 2022-06-20 09:30  wjxuriel  阅读(116)  评论(0)    收藏  举报