Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
JavaScript Program to Find LCM (5 Ways)
The Least Common Multiple (LCM) of two or more integers is the smallest positive integer that is divisible by each of the numbers. For example:
-
The LCM of 4 and 6 is 12 because 12 is the smallest number that is divisible by both 4 and 6.
LCM is commonly used in problems involving fractions, ratios, and scheduling. In this tutorial, you will learn how to calculate the LCM in JavaScript using various methods. If you're wondering how to find LCM in JavaScript, this guide provides multiple approaches and examples for efficient computation.
JavaScript Programs to Find LCM Using Formula (HCF and LCM Relation)
The relationship between HCF and LCM is given by:
LCM(a, b) = (a * b) / HCF(a, b)
This program calculates the LCM using the above formula.
Code
function findHCF(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function findLCMUsingHCF(a, b) {
return (a * b) / findHCF(a, b);
}
// Test the function
const num1 = 4;
const num2 = 6;
console.log(`The LCM of ${num1} and ${num2} is:`, findLCMUsingHCF(num1, num2));
Output
The LCM of 4 and 6 is: 12
Explanation
- The HCF (Greatest Common Factor) is computed using a loop and the modulus operation.
- The LCM is then calculated by dividing the product of the two numbers by their HCF.
Find LCM in JavaScript Using Iterative Method
This method finds the LCM by incrementing the larger number until it is divisible by both numbers.
Code
function findLCMUsingIteration(a, b) {
let lcm = Math.max(a, b);
while (true) {
if (lcm % a === 0 && lcm % b === 0) {
return lcm;
}
lcm++;
}
}
// Test the function
console.log(`The LCM of ${num1} and ${num2} is:`, findLCMUsingIteration(num1, num2));
Output
The LCM of 4 and 6 is: 12
Explanation
-
The algorithm starts with the larger of the two numbers.
-
It increments the candidate LCM by 1 until it satisfies the divisibility condition for both numbers.
-
This method is simple but may not be the most efficient for large inputs.
Print LCM in JavaScript Using while Loop and if Statement
This program uses a while loop along with an if statement to calculate the LCM.
Code
function findLCMUsingWhileIf(a, b) {
let lcm = Math.max(a, b);
while (lcm % a !== 0 || lcm % b !== 0) {
lcm++;
}
return lcm;
}
// Test the function
console.log(`The LCM of ${num1} and ${num2} is:`, findLCMUsingWhileIf(num1, num2));
Output
The LCM of 4 and 6 is: 12
Explanation
The program starts with the larger of the two numbers and increments until it satisfies the divisibility condition for both inputs.
LCM Calculation in JavaScript Using HCF
This method demonstrates a direct computation of LCM using the relationship between HCF and LCM.
Code
function findLCMDirectlyUsingHCF(a, b) {
const hcf = (x, y) => {
while (y) {
[x, y] = [y, x % y];
}
return x;
};
return (a * b) / hcf(a, b);
}
// Test the function
console.log(`The LCM of ${num1} and ${num2} is:`, findLCMDirectlyUsingHCF(num1, num2));
Output
The LCM of 4 and 6 is: 12
Explanation
-
The HCF is calculated first using a while loop.
-
The LCM is then derived using the formula .
JavaScript Program to Find the LCM of an Array of Numbers
This program calculates the LCM for an array of numbers by applying the LCM formula iteratively.
Code
function findLCMOfArray(numbers) {
const findHCF = (a, b) => {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
};
const findLCM = (a, b) => (a * b) / findHCF(a, b);
return numbers.reduce((acc, num) => findLCM(acc, num));
}
// Test the function
const numbers = [4, 6, 8];
console.log(`The LCM of [${numbers.join(", ")}] is:`, findLCMOfArray(numbers));
Output
The LCM of [4, 6, 8] is: 24
Explanation
-
This method finds the LCM of an array by iteratively applying the LCM formula.
-
The reduce function is used to apply the LCM calculation across all elements in the array.
-
For the array [4, 6, 8]:
-
LCM of 4 and 6 is 12.
-
LCM of 12 and 8 is 24.
Concepts Used in Above Programs
Relationship Between HCF and LCM
-
The formula is used to derive the LCM from the HCF.
Iterative and Conditional Logic
-
while loops and if statements are used to incrementally find the LCM.
Array Operations
-
The reduce method is used to calculate the LCM for multiple numbers in an array.