Signup/Sign In
PUBLISHED ON: JANUARY 17, 2023

JavaScript Program to Find LCM of Two Numbers

We will learn how to write a JavaScript program to find the least common multiple (LCM) of two numbers in this tutorial. The smallest positive integer that is divisible by both of the given numbers is the LCM. The program will take two numbers from the user and calculate the LCM using mathematical algorithms.

The steps for writing the program, including the logic and syntax used to find the LCM, will be covered in this tutorial. You will be able to write a JavaScript program that can find the LCM of any two numbers by the end of this tutorial. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

What is Least Common Multiple(LCM)?

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 10 and 12 is 60.

The LCM of two numbers can be found using the given Formula:

LCM = (num1*num2) / HCF

Program to Find LCM of Two Numbers

// JavaScript program to find the LCM of two integers

var hcf;

const num1 = prompt('Enter a first integer: ');
const num2 = prompt('Enter a second integer: ');

while(num1 != num2){
    if(num1 > num2) 
        num1 -= num2;
    else 
        num2 -= num1;
}
hcf = num1; 
// find LCM
let lcm = (num1 * num2) / hcf;

console.log("LCM of the two numbers is ", lcm); 


Enter a first positive integer: 10
Enter a second positive integer: 12
The LCM of the numbers is 60

Conclusion

Finally, we saw how to create a JavaScript program to find the least common multiple (LCM) of two numbers. The program takes two numbers from the user and calculates the LCM using mathematical algorithms. We've gone over the logic and syntax used in the program, which involves calculating the LCM by finding the greatest common divisor (GCD) of two numbers.

You should now be able to write your own JavaScript program to find the LCM of any two numbers by following the steps outlined in this tutorial. This can be useful in a variety of mathematical and computational applications, as well as in better understanding the concept of LCM.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.