Site icon T4Tutorials.com

Program to find the LCM of a number in PHP with database and form

Program to find the LCM of a number in PHP with database and form

In this tutorial, we will learn about the followings.

  1. Program to find the LCM of a number in PHP
  2. Program to find the LCM of a number in PHP with form
  3. Program to find the LCM of a number in PHP with database

Program to find the LCM of a number in PHP with form.

In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.

<html>
<body>
<form method="POST"> 
Enter  First No. <input type="text" name="num1">
Enter Second NO.<input type="text" name="num2"><br><br> 
Result: <input type="submit" name="Ok">
</form>
</body>
</html>
<?php 
if (isset($_POST['Ok'])) {
  $num1=$_POST['num1'];
  $num2=$_POST['num2'];
  $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;
    }
    }
     //echo '$max';

?>

Program to find the LCM of a number

In this example, we will take the inputs from the user and store them in variables.

<?php 
  $num1=3;
  $num2=4;
  $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;
    }
?>

Program to find the LCM of a number in PHP with a database.

In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.

<html>
<body>
<form method="POST"> 
Enter  First No. <input type="text" name="num1">
Enter Second NO.<input type="text" name="num2"><br><br> 
Result: <input type="submit" name="Ok">
<br><br>
</form>
</body>
</html>
<?php 
$conn=mysqli_connect('localhost','root','','lcm');
if (mysqli_connect_error())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
if (isset($_POST['Ok'])) {
  $num1=$_POST['num1'];
  $num2=$_POST['num2'];
  $max=($num1>$num2) ? $num1 : $num2;
    while(1)
    {
      if($max%$num1==0 && $max%$num2==0)
        {
          echo "<input value=$max name=max><br>";
          break;
        }
    $max=$max+1;
   
    }
     mysqli_query($conn,"INSERT INTO lcm (First,Second,Result) 
VALUES ('$num1','$num2',$max)");
    mysqli_close($conn);
}
?>

Database for finding the LCM of a number. 

Figure: LCM of a number in PHP with database and form.
Exit mobile version