LCM Program in Java with Explanations

In this tutorial, we are going to learn java program to calculate the least common multiple of two numbers.

Our program will take two numbers as the input from the user and return the LCM of input numbers.

For example

if the user inputs the numbers 4 and 6. then the output should be ‘12’ as 12 is the LCM of 4 and 6.

Program 1: Calculate LCM of two numbers in Java

In the below program we are following the traditional approach by taking two numbers as input from the user and iteratively checking for the LCM. We are using while loop to iterate and increments the maximum of the two numbers until it finds a number that is evenly divisible by both. The output for the input numbers 54 and 4 was correctly calculated as 108.

import java.util.*;
public class Main
{
    public static void main(String[] args) {
        double num1,num2,maxNum;
        System.out.println("Java Program to calculate LCM" );
        Scanner sc = new Scanner(System.in);
        System.out.println("Please give first number");
        num1= sc.nextDouble();
        System.out.println("Please give second number");
        num2 = sc.nextDouble();
        maxNum = (num1 > num2) ? num1 : num2;
        while (true) {
            if (maxNum % num1 == 0 && maxNum % num2 == 0) {
            System.out.println("LCM = "+maxNum );
            break;
        }
        ++maxNum;
    }
}
}

Output

Java Program to calculate LCM
Please give first number
54
Please give second number
4
LCM = 108.0

Program 2: Calculate LCM of two numbers using Java 8

In the below program we are using Java 8 streams and functional programming concepts to calculate LCM. The calculateLCM method used to calculate GCD by calling calculateGCD method. And then applying the LCM formula. The output for the input numbers 54 and 4 was again correctly computed as 108.

import java.util.Scanner;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please give first number:");
        int num1 = scanner.nextInt();

        System.out.println("Please give second number:");
        int num2 = scanner.nextInt();

        int lcm = calculateLCM(num1, num2);

        System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
    }

    private static int calculateLCM(int num1, int num2) {
        int gcd = calculateGCD(num1, num2);
        return (num1 * num2) / gcd;
    }

    private static int calculateGCD(int a, int b) {
        return IntStream.rangeClosed(1, Math.min(a, b))
                .filter(i -> a % i == 0 && b % i == 0)
                .max()
                .orElse(1);
    }
}

Output

Please give first number:
54
Please give second number:
4
LCM of 54 and 4 is: 108

Conclusion


In the above tutorial we have seen how to calculate the Least Common Multiple (LCM) of two numbers using Java. LCM represents the smallest common multiple that is divisible by both input numbers.

In the above program we have seen both logics building as well using Java 8 stream api concepts.

You are now equipped with two different methods to calculate the LCM of two numbers in Java. Whether using a traditional iterative approach or leveraging the functional capabilities of Java 8 streams, you have the flexibility to choose the method that best fits your coding preferences and requirements.

Happy coding!