Showing posts with label php pascal triangle. Show all posts
Showing posts with label php pascal triangle. Show all posts

Tuesday, March 3, 2015

Pascal Triangle in PHP

In this short tutorial I will show you how to create a Pascal Triangle Checker in PHP. What is program will do is very simple it will ask the user to enter a number and then our program will generate the corresponding Pascal Triangle Numbers based on the given number by our user.

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> Pascal Triangle Checker</title>
<style>
body { 
  font-size:25px; 
  font-family:arial;
  color:blue;
  } 
  
#banner {
  width: 750px;
  background-color: yellow;
  height: 100px;
  margin: 0 auto;
  position: inherit;
  top: 100; 
  left: 0; 
  bottom:0; 
  right: 0;
  padding: 16px 16px;
  
  border-radius: 75px;
  border: 5px solid #fba827;
}
  /* BUTTONS */

#submit {
  background: #0a7d66;
  background-image: -moz-linear-gradient(#0fb493, #0a7d66);
  background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #6cbb6b),color-stop(1, #0fb493));
  
  margin: 0 0 0 2px;
  padding: 15px 20px;

  border-radius: 3px 70px 70px 3px;
  -moz-border-radius: 3px 70px 70px 3px;
  border-color: #0fb493 #0da284 #0c9075;

  -moz-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
  -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;  

  font: 16px 'Droid Serif', serif;
  background: #0fb493;
  color: blue;
  cursor: pointer;

  text-shadow: 0 5px 0 rgba(255,255,255,0.4);
}

#submit:hover {       
    background-color: #0fb493;
    background-image: linear-gradient(#0a7d66, #0fb493);
}   

#submit:active {       
    background: #0fb493;
    outline: none;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;        
}

#submit::-moz-focus-inner {
       border: 0;  /* Small centering fix for Firefox */
}
.inputs {
    -webkit-border-radius: 20px 3px 3px 20px;
    -moz-border-radius: 20px 3px 3px 20px;
    -ms-border-radius: 20px 3px 3px 20px;
    -o-border-radius: 20px 3px 3px 20px;
    border-radius: 20px 3px 3px 20px;

    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -ms-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
    background: yellow;
    border: 5px solid #fba827;
    color: blue;
    font: 22px 'Droid Serif', serif;
    margin: 0 0 10px;
    padding: 15px 20px 15px 20px;
    width: 150px; 
}
</style>
<?php
error_reporting(0);
$values = $_POST['value'];

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

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


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

  
 }
   }  


?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
   <div id="banner">
          <h1 align="center">Pascal Triangle Checker</h1>
      </div>
  <br>
<form action="" method="post">
 Enter a Number : <input id="text" type="text" class="inputs"  type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=5 required/>
   <input  id="submit"  type="submit"  name="check" value="Check Pascal Triangle" 
  title="Click here check for Pascal Triangle Values."/>
  <input id="submit"  type="submit" name="clear" value="Clear" 
  title="Click here to clear text box and values on the screen"/>
</form>

<?php 
echo "<br>";
$rows=$values;
$coef=1;
$space=0;
$a=0;
$j=0;
    
    for($a=0;$a<$rows;$a++)
    {
        for($space=1;$space<=$rows-$a;$space++)
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        for($j=0;$j<=$a;$j++)
        {
            if ($j==0||$a==0)
                $coef=1;
            else
               $coef=$coef*($a-$j+1)/$j;
echo "<font size='6' face='arial'>";   
            echo " &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
     .$coef."&nbsp;&nbsp";
echo "</font>";
        }
        echo "<br>";
    }
 
?>
  </body>
</html>