Find GCD of Two Numbers using Recursion
In this approach, the recursive function GCD calculates Greatest Common Divisor using the Euclidean algorithm. If “b = 0”, it returns “a”, otherwise, recursively calls with b and remainder of a/b.
Syntax:
function GCD(a, b) {
if ( b === 0 ) {
return a;
}
return GCD(b, a % b);
}
Example: In this example, we are using the above-explained approach.
PHP
<?php function GCD( $a , $b ) { if ( $b === 0) { return $a ; } return GCD( $b , $a % $b ); } $num1 = 28; $num2 = 35; $result = GCD( $num1 , $num2 ); echo $result ; ?> |
Output
7
PHP Program to Find GCD or HCF of Two Numbers
Given two numbers, the task is to find the GCD or HCF of two numbers in PHP. GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both numbers without leaving a remainder.