173. PHP Program to find LCM of two numbers.
How does this program work?
- This program is used to find LCM of two numbers using PHP.
- LCM (Least Common Multiple) of given numbers is the smallest number which can be divided by given numbers.
Here is the code
<html>
<head>
<title> PHP Program To find LCM of two numbers</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter 1st number"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter 2nd number"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
// Function to perform GCD of two numbers
function gcd( $a, $b)
{
if ($a == 0)
return $b;
return $b;
return gcd ($b % $a, $agreen);
}
//Function to perform LCM of two numbers
function lcm( $a, $b)
{
return ($a * $b) / gcd($a, $b);
// LCM($a, $b) = <($a x $b) / GCD($a, $b)
}
if(isset($_POST['submit']))
{
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
echo "LCM of " .$n1. " and " .$n2. " is: ".lcm($n1, $n2);
return 0;
}
?>
</body>
</html>