Friday, December 29, 2017

phpMyPassion

Fibonacci Series in PHP


Fibonacci series means to get next term by adding its previous two numbers.

For an example:-

0 1 1 2 3 5 8 13 21

Here I am writing the PHP program to print Fibonacci Series.

<?php

function printFabonacciSeries($number){
    $a = 0; // first number of series
    $b = 1; // second number of series
    echo 'Fabonacci Series <br/>';
    echo $a.' '.$b;
    for($i=2;$i<$number;$i++){
        $c = $a + $b; //print next number of series
        echo ' '.$third;
        $a = $b;
        $b = $c;
    }
}
printFabonacciSeries(9);
?>

Output :- 0 1 1 2 3 5 8 13 21

Alternate Way to Print Fabonacci Series :-

<?php
$number = 9;
$first = 0;
$second = 1;
echo "$first $second "; // 0 1

for($i=1;$i<= $number-2 ;$first=$second,$second=$third,$i++ ) 
{
  echo $third = $first+$second; //print next number of series
  echo " ";
}
?>

Using the above program you can find Fibonacci Series in PHP.



About Author -

Hi, I am Anil.

Welcome to my eponymous blog! I am passionate about web programming. Here you will find a huge information on web development, web design, PHP, Python, Digital Marketing and Latest technology.

Subscribe to this Blog via Email :

Note: Only a member of this blog may post a comment.