Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions rohit/js/find-hcf-lcm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function findHCF(a, b) {
let hc = 1;
for (let itr = 2; itr <= (a && b); itr++) {
if (a % itr == 0 && b % itr == 0) {
hc = itr;
}
}

return hc;
}

console.log("LCM of 45,60 = " + findHCF(45, 60));

function findLCM(a, b) {
let lc = (a * b) / findHCF(a, b);

return lc;
}

console.log("LCM of 10,45 = " + findLCM(10, 45));

// find HCF by using Euclidean's Law
function getHCF(a, b) {
if (b == 0) {
return a;
}
return getHCF(b, a % b);
}

// get HCF of two numbers
console.log("HCF of 30,45 = " + getHCF(30, 45));

// get HCF of three numbers
console.log("HCF of 30,45,60 = " + getHCF(getHCF(30, 45), 60));

// get LCM of three numbers
console.log("LCM of 30,40,60 = " + findLCM(findLCM(30, 40), 60));