Find LCM of Two Numbers in PHP using While loop
ADVERTISEMENTS
Find lcm of two numbers in PHP using while loop. In this article, you will learn how to find lcm of two numbers in php using while loop.
Find LCM of Two Numbers in PHP using While loop
<?php
// Find LCM of Two Numbers in PHP using While loop
$p = 175;
$q = 165;
$x = 0;
// $p & $q - To store the two positive numbers
// $x - To store the LCM number
$x = $p > $q ? $p : $q;
while (1) {
if ($x % $p == 0 && $x % $q == 0) {
echo "The LCM of the {$p} and {$q} numbers is {$x}.";
break;
}
++$x;
}
?>
Output
The LCM of the 175 and 165 numbers is 5775.
Find LCM of Two Numbers in PHP using For loop
<?php
// Find LCM of Two Numbers in PHP using For loop
$p = 175;
$q = 185;
$x = 0;
$g = 0;
// $p & $q - To store the two positive numbers
// $x - To store the LCM number
// $g - To store the GCD
for ($i = 1; $i <= $p && $i <= $q; $i++) {
if ($p % $i == 0 && $q % $i == 0)
$g = $i;
}
$x = ($p * $q) / $g;
echo "The LCM of two positive numbers {$p} & {$q} is {$x}.";
?>
Output
The LCM of two positive numbers 175 & 185 is 6475.