JavaScript Program to Find LCM

To understand this example, you should have the knowledge of the following JavaScript programming topics:


The Least Common Multiple (LCM) of two integers is the smallest positive integer that is perfectly divisible by both integers.

For example, the LCM of 6 and 8 is 24.


Example 1: LCM Using while Loop and if Statement

Output

Enter a first positive integer: 6
Enter a second positive integer: 8
The LCM of 6 and 8 is 24

In the above program, the user is prompted to enter two positive integers.

The greater number among the numbers provided by the user is stored in a min variable. The LCM of two numbers cannot be less than the greater number.

The while loop is used with an if statement. In each iteration,

  • The variable min is divided by both the num1 and num2.
  • If both numbers' remainders are equal to 0, then it is the LCM and the break statement terminates the program.
  • If both numbers' remainders are not equal to 0, the value of min is increased by 1 and the loop continues.
  • The while loop continues until the condition is met.
    if (min % num1 == 0 && min % num2 == 0)

The LCM of two numbers can also be found using the formula:

LCM = (num1*num2) / HCF

To learn about how to find the HCF, visit the JavaScript program to find HCF.

Example 2: LCM Calculation Using HCF

Output

Enter a first positive integer: 6
Enter a second positive integer: 8
The LCM of 6 and 8 is 24.

In the above program, firstly HCF of the numbers is calculated. Then LCM is calculated using the given formula.