Friday, February 27, 2015

Floyd's Triangle Maker

In this short article I would like to share with you a sample program that I wrote using PHP as our programming language I called this program Floyd's Triangle Maker what this program will do is to ask the user to enter a number and then it will generate the floyd's triangle on our screen. To give you some insights floyd's triangle was developed by an american computer scientist and mathematician named Robert Floyd. 

According to Wikipedia Floyd's Triangle is a right-angled triangular array of natural numbers, used in computer science education. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.

1
2    3
4    5     6
7     8     9  10
11  12  13  14 15

Sample of Floyd's Triangle

If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360



Sample Output of Our Program

Program Listing

<html>
<title> Floyd's Triangle Maker</title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>
<?php

$values = $_POST['value'];

 function floyds_triangle($variable) 
{
 $a = 1;   
 for ($i = 1; $i <= $variable; $i++)
  {
    for ($c = 1; $c <= $i; $c++)
    {
      echo " ".$a." ";
      $a++;
    }
  echo '<br><br>';
  }
     
}


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

$title = "======= FLOYD'S TRIAGLE =======";
    if (ctype_alpha($values)) {
      echo "<script>";  
  echo "alert('PLEASE ENTER A NUMERIC VALUE.');";
  echo "</script>";
  $values="";
  $title=""; 
           $result="";
 }
    else  if ($values=="") {
      echo "<script>";  
  echo "alert('It Cannot Be Empty!!! Please enter a integer value.');";
  echo "</script>";
  $title=""; 
  $values="";
           $result="";
 }
   }  

if(isset($_POST['clear'])) {
  $title=""; 
  $values=" "; 
  $result="";
  
}

?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center">Floyd's Triangle Maker</h2>
  <br>
<form action="" method="post">
 Enter a Number : <input type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=5 required/>
   <input type="submit" name="check" value="Find Floyd's Triangle" 
  title="Click here to view the list of Floyd's Triangle Number."/>
  <input type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>

<br> <br>

<?php 

echo $title;
echo "<br><br>";
floyds_triangle($values); 

 ?>
 </body>
</html>



No comments:

Post a Comment