Showing posts with label fibonacci in php. Show all posts
Showing posts with label fibonacci in php. Show all posts

Friday, January 23, 2015

Fibonacci Number Series Generator in PHP

In this article I would like to share with you one of a sample program that I wrote just to learn and practice my skills in PHP programming I called this program Fibonacci Number Series Generator in PHP. What the program will do is to ask the user to enter a number and then our program will generate the fibonacci number series in a given number by our user.

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

People here in the Philipines who wish to contact me can reach me at 09173084360.

Thank you very much and Happy Programming



Sample Output of Our Program

Program Listing

<html>
<title> Fibonacci Number Series Generator </title>
<style>
body { 
  font-size:20px; 
  font-family:arial;
  color:blue;
  } 
</style>
<?php

$values = $_POST['value'];

   
 function fibonacci($variable) 
{
   $counter = 0 ; $fibo1 = 0; $fibo2 = 1;
   
   echo $fibo1." , ";  echo $fibo2." , "; 

  while ($counter < $variable ) 
  {
     $fibo3 = $fibo2 + $fibo1 ; 
     echo $fibo3." , "; 
     $fibo1 = $fibo2 ;
     $fibo2 = $fibo3 ; 
     $counter+=1;
  } 
}


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

$title = "List of Fibonacci Number Series";

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

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

?>
<body style='background-color:lightgreen'>
<div style='width:800px;margin:auto'>
  <br> <h1 align="center">Fibonacci Number Series Generator</h2>
  <br>
<form action="" method="post">
 Enter a Number : <input type="text" name="value"    value="<?php echo $values; ?>" autofocus  size=20/>
   <input type="submit" name="check" value="Find Fibonacci Number" 
  title="Click here to view the list of Fibonacci Number Series."/>
  <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>";
fibonacci($values);
 ?>
  </body>
</html>