Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

Write a PHP program that accepts two numbers using a web form and calculates greatest common divisor (GCD) and least common multiple (LCM) of entered numbers.(Use recursive function)

<form method="post">   
    Enter a Number 1: <input type="number" name="t1"><br><br>   
    Enter a Number 2: <input type="number" name="t2"><br><br>   
    <input type="submit" name="submit" value="Submit">   
</form> 

<?php 
if (isset($_POST['submit'])) {
    $x = $_POST['t1']; 
    $y = $_POST['t2']; 

    function gcd($x, $y)  
    { 
        if ($y == 0) 
            return $x; 
        return gcd($y, $x % $y); 
    }  

    $lcm = ($x * $y) / gcd($x, $y); 
    echo "LCM of $x and $y is: $lcm"; 
}
?>

Post a Comment

0 Comments