Java Code Example: least common multiple

The least common multiple of two integers a and b is the smallest number that is both an integer multiple of a and also an integer multiple of b.

public class LeastCommonMultiple {
    static int gcd(int a, int b) {
        if (a == 0) {
            return b;
        } else if (b == 0) {
            return a;
        } else if (a > b) {
            return gcd(a - b, b);
        } else {
            return gcd(a, b - a);
        }
    }

    static int lcm(int a,int b) {
        return a * b / gcd( a, b);
    }

    public static void main(String[] args) {
        int a = 10, b = 6;

        System.out.println("Least common multiple is: " + lcm(a, b));
    }
}
Output
Least common multiple is: 30