Fibonacci Series Program in PHP
Advertisements
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
Google Advertisment