Skip to content

Commit 0dffb98

Browse files
committed
hcf.js
1 parent fd1533a commit 0dffb98

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

  • javascript/3_math-programs

javascript/3_math-programs/hcf.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 8. Find GCD or HCF of Two Numbers
2+
// hcf = product of numbers / LCM of numbers
3+
4+
function hcf(a,b){
5+
let smaller = a > b ? b : a;
6+
let hcf = 1;
7+
for(let i = 1;i <= smaller; i++){
8+
if(a % i === 0 && b % i === 0){
9+
hcf = i;
10+
}
11+
}
12+
13+
return hcf;
14+
15+
}
16+
17+
console.log(hcf(10,20))
18+
19+
20+
// using recursion
21+
22+
function hcfRecursion(a,b){
23+
if(b === 0) return a;
24+
return hcfRecursion(b, a%b);
25+
}
26+
27+
console.log(hcfRecursion(30,45))
28+
29+
30+

0 commit comments

Comments
 (0)