GitHub

https://github.com/Choidongjun0830

Java

[Java] LCM 찾기

gogi masidda 2023. 8. 3. 16:22
public class BiNumber {

    private int number1;
    private int number2;

    public BiNumber(int number1, int number2) {
        this.number1 = number1;
        this.number2 = number2;
    }

    public int getNumber1() {
        return number1;
    }

    public int getNumber2() {
        return number2;
    }

    public int calculateLCM() {
        // TODO: Write your code here
        if(number1 < 0 || number2 < 0){
            return -1;
        } else if(number1 == 0 || number2 == 0){
            return 0;
        }
        int max = Math.max(number1, number2);
        int min = Math.min(number1, number2);
        int lcm = max;
        int num = 1;
        while(lcm % min != 0){
            lcm = max * num;
            num++;
        }
        return lcm;
    }
}
728x90