Open In App

JavaScript Program to Find LCM of Two Numbers

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about finding the LCM of two numbers by using JavaScript. LCM (Least Common Multiple) of two numbers is the smallest positive integer that is divisible by both numbers without leaving a remainder. It’s a common multiple of the numbers.

 LCM of two numbers = Product of two numbers ÷ HCF of two numbers.
LCM(a, b) = (a * b) / GCD(a, b)
Where GCD(a, b) represents the Greatest Common Divisor of the two numbers.

Example:

Numbers : 12 and 80
prime factors of each
12: 2 × 2 × 3
80: 2 × 2 × 2 × 2 × 5 (2: 4 times,3: 1 time,5: 1 time)
Multiply each factor the maximum number of times it occurs in either number.
2 x 2 x 2 x 2 x 3 x 5 =240 ( 240 is the lowest number that can be divided by both 12 and 80.)

There are several methods that can be used to Check if a Number is Odd or Even, which are listed below:

  • Using the Formula (a * b) / GCD(a, b)
  • Without using the formula
  • Using a Loop

We will explore all the above methods along with their basic implementation with the help of examples.

JavaScript Program to Find LCM of Two Numbers using Formula (a * b) / GCD(a, b)

In this approach, using the formula (a * b) / GCD(a, b). It calculates the Greatest Common Divisor (GCD) using the Euclidean algorithm, then applies the formula to determine the LCM. This ensures the obtained LCM is the smallest multiple divisible by both input numbers.

Syntax:

function lcmFunction( a, b ) {
const gcdValue = gcd( a, b );
return (a * b) / gcdValue;
}

Example: In this example, GCD is calculated using a loop. LCM is determined via formula. Accurate LCM result ensures divisibility by both numbers (12, 18).

Javascript




function gcd(a, b) {
    for (let temp = b; b !== 0;) {
        b = a % b;
        a = temp;
        temp = b;
    }
    return a;
}
  
function lcmFunction(a, b) {
    const gcdValue = gcd(a, b);
    return (a * b) / gcdValue;
}
  
let num1 = 12;
let num2 = 18;
let lcm = lcmFunction(num1, num2);
console.log("LCM of given numbers is :", lcm);


Output

LCM of given numbers is : 36

JavaScript Program to Find LCM of Two Numbers without using the formula

In this approach, we are finding the LCM of two numbers without using GCD. It iterates from the larger number and checks for divisibility by the smaller number, providing accurate LCM.

Syntax:

function findLCM(a, b) {
let lar = Math.max(a, b);
let small = Math.min(a, b);
for (i = lar; ; i += lar) {
if (i % small == 0)
return i;
}
};

Example: In this example, we are using the above-explained approach.

Javascript




// JavaScript program to find LCM of 2 numbers
// without using GCD
  
// Function to return LCM of two numbers
function findLCM(a, b) {
    let lar = Math.max(a, b);
    let small = Math.min(a, b);
    for (i = lar; ; i += lar) {
        if (i % small == 0)
            return i;
    }
}
  
// Driver program to test above function
let a = 5, b = 7;
console.log("LCM of " + a + " and "
    b + " is " + findLCM(a, b));


Output

LCM of 5 and 7 is 35

JavaScript Program to Find LCM of Two Numbers using a Loop

In this approach, we are using a loop to find the LCM of two numbers. Starting from the larger number, it increments by that value until it finds a multiple that is divisible by the smaller number, resulting in the LCM

Syntax:

function lcmFunction(a, b) {
let larger = Math.max(a, b);
let smaller = Math.min(a, b);
for (let i = larger; ; i += larger) {
if (i % smaller === 0) {
return i;
}
}
};

Example: In this example, we are using the above-explained approach.

Javascript




function lcmFunction(a, b) {
    let larger = Math.max(a, b);
    let smaller = Math.min(a, b);
    for (let i = larger; ; i += larger) {
        if (i % smaller === 0) {
            return i;
        }
    }
}
  
let num1 = 12;
let num2 = 18;
let result = lcmFunction(num1, num2);
console.log(`LCM of ${num1} and ${num2} is ${result}`);


Output

LCM of 12 and 18 is 36


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads