๐ŸŽฏ Overview

Write a Java program to find the Lowest Common Multiple(LCM) of two numbers.

Important concepts to be aware of:

  • product of two numbers = their gcd x their lcm

๐ŸŽฏ Solving the problem in Java

/**
 * Problem 10: Write a Java program to find the Lowest Common Multiple(LCM) of two numbers.
 */
public class Problem_10 {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the first number:");
    int firstNumber = sc.nextInt();

    System.out.println("Enter the second number:");
    int secondNumber = sc.nextInt();

    System.out.println("LCM is: " + calculateLCM(firstNumber, secondNumber));
  }

  private static int calculateLCM(int firstNumber, int secondNumber) {
    int lcm = (firstNumber * secondNumber) / calculateGCD(firstNumber, secondNumber);
    return lcm;
  }

  private static int calculateGCD(int firstNumber, int secondNumber) {
    if(firstNumber == 0) {
      return secondNumber;
    }
    return calculateGCD(secondNumber % firstNumber, firstNumber);
  }
}

Output:

Enter the first number:
12
Enter the second number:
24
LCM is: 24

Process finished with exit code 0

๐ŸŽฏ Github

Code shared in this post can be found here.