Fibonacci Series Program in PHP


Prev Tutorial Next Tutorial

Fibonacci Series Program in PHP

The simple concept to find fibonacci series is; add two previous term and get next term. Example of Fibonacci Series is; 0 1 1 2 3 5.

Fibonacci Series Program in PHP

<?php

function printFibonacci($n)
 {
 
  $first = 0;
  $second = 1;
 
  echo "Fibonacci Series \n";
 
  echo $first.' '.$second.' ';
 
  for($i = 2; $i < $n; $i++){
 
    $third = $first + $second;
 
    echo $third.' ';
 
    $first = $second;
    $second = $third;
 
    }
}
  
/* Function call to print Fibonacci series upto 6 numbers. */
 
printFibonacci(6);

?>

Output

	
Fibonacci Series 0 1 1 2 3 5

Fibonacci Series Program in PHP

<?php

define('NUM',11);
$a = 0;
$b = 1;

echo "$a $b "; // 0 1

for($i=1   ; $i<= NUM-2 ;  $a=$b, $b=$c, $i++ ) 
{
  echo $c = $a+$b;
  echo " ";
}

?>

Output

	
0 1 1 2 3 5 8 13 21 34 55

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements