Find LCM in JavaScript

This is a preview lesson
Register or sign in to take this lesson.

This tutorial shows how to find the LCM of two numbers in JavaScript. The Least Common Multiple(LCM) of two numbers a and b is the smallest positive integer that is evenly divisible by both a and b. In JavaScript, we can use While loop and if…else condition to find the LCM of two numbers.

Example: Find the LCM of Two Numbers in JavaScript

// program to find the LCM of two integers

function FindLCM(a,b){
    let min;
    if(a>b){
        min=a;
    }
    else{
        min=b;
    }

    // loop till you find a number by adding the largest number which is divisible by the smallest number
    while (true) {
        if (min % a == 0 && min % b == 0) {
            return min;
            break;
        }
        min++;
    }
}

console.log(FindLCM(6,8))
console.log(FindLCM(4,2))

Output

24
4