Code for finding GCD or HCF
Note: GCD and HCF are same GCD: Greatest common divisor HCF: Highest common factor
Using Recursion
function gcd(a, b) {if (b === 0) return a;return gcd(b, a % b);}gcd(15, 20); // 5
Using FOR loop
function gcd(a, b) {let hcf = -1;for (let i = 1; i <= a && i <= b; i++) {if (a % i == 0 && b % i == 0) {hcf = i;}}return hcf;}gcd(15, 20); // 5
Using WHILE loop
function gcd(a, b) {while (a != b) {if (a > b) {a -= b;} else {b -= a;}}return a;}gcd(15, 20); // 5