LCM stands for “Least Common Multiple”.
The least common multiple (LCM) of two numbers is the smallest number (not zero) that is a multiple of both.
Let’s see the example –
Q. Find LCM of 3 and 4
A. First check multiply of both numbers.
Multiply of 3 are :
0,3,6,9,12,15,18,21,24…..
Multiply of 4 are :
0,4,8,12,16,20,24…..
In the above common multiply of 3 and 4 are – 0,12,24…
But as i already tell LCM of two numbers is the smallest number (not zero) that is a multiple of both,so we except 0 from above result then we get LCM of 3 and 4 is 12.
Now solve this by using PHP
<?php
$num1=3; // user input
$num2=4; // user input
$max=($num1>$num2) ? $num1 : $num2;
while(1)
{
if($max%$num1==0 && $max%$num2==0)
{
echo "LCM of " .$num1. " and " .$num2. " is: ".$max;
break;
}
$max=$max+1;
}
// Output
LCM of 3 and 4 is: 12