We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fd1533a commit 0dffb98Copy full SHA for 0dffb98
1 file changed
javascript/3_math-programs/hcf.js
@@ -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