Showing posts with label greatest common divisor in php. Show all posts
Showing posts with label greatest common divisor in php. Show all posts

Friday, August 5, 2016

Greatest Common Divisor in PHP

Hi there in this article I would like to share with you a program that I wrote in PHP to compute for the Greatest Common Divisor. What does our program will do is to ask the user to give two numbers and then it will compute it greatest common divisor equivalent.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.




Sample Program Output


Program Listing


<html>
<title>  Greatest Common Divisor Solver in PHP  </title>
<style>
  body {
   font-family: arial;
   font-size: 18px;
   font-weight: bold;
 };
</style>
<body>
  <?php
   error_reporting(0);
  $input_A = $_POST['inputFirst'];
  $input_B = $_POST['inputSecond'];
  if(isset($_POST['ClearButton'])){
  $input_A="";
  $input_B="";
  $display_result="";
  }

   ?>
   <br>
  <h2> Greatest Common Divisor Solver in PHP </h2>
<form action="" method="post">
  Give first number
  <input type="text" name="inputFirst"
  value="<?php echo $input_A; ?>" size="5" autofocus required/>
<br><br>
  Give second number
  <input type="text" name="inputSecond"
  value="<?php echo $input_B; ?>" size="5" required/>
  <br><br>

  <input type="submit" name="SubmitButton" value="Ok"/>
   &nbsp;&nbsp;&nbsp;
  <input type="submit" name="ClearButton" value="Clear"/>
</form>
<?php


function GCD($a, $b)
{
if ($a == 0)
return $b;

while ($b != 0)
{
if ($a > $b)
$a -= $b;
else
$b -= $a;
}

return $a;
}


if(isset($_POST['SubmitButton'])){

$value = GCD($input_A,$input_B);

  echo "<br>";
  $display_result = "The Greatest Common Divisor between "
                    .$input_A. " and  ".$input_B. " is "
                    .$value.".";

  echo $display_result;
  echo "<br>";
 }

?>
</body>
</html>