Find LCM and GCD: Write a PHP function to find the least common multiple (LCM) and greatest common divisor (GCD) of two numbers.

function findLCM($a, $b) {
    return ($a * $b) / gcd($a, $b);
}

function gcd($a, $b) {
    while ($b != 0) {
        $temp = $b;
        $b = $a % $b;
        $a = $temp;
    }
    return $a;
}

// Example usage:
$num1 = 12;
$num2 = 18;
echo "LCM: " . findLCM($num1, $num2);

Related Posts

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Artificial Intelligence