Fibonacci Series

fibonacci series
PHP

Fibonacci Series

Write a program to find the fibonacci series in php.

In Fibonacci series each number is the sum of the preceding ones.

Eg:- 0 1 1 2 3 5 8 13 121
start with 0 and 1, then
0+1 = 1, 1+1 = 2, 1+2 = 3, so on

Program :

<?php  
$num = 0;  
$num1 = 0;  
$num2 = 1;  
echo $num1.' '.$num2.' ';  
while ($num < 10 )  
{  
    $num3 = $num2 + $num1;  
    echo $num3.' ';  
    $num1 = $num2;  
    $num2 = $num3;  
    $num = $num + 1;  
}
?>  
  1. Define $num = 0, $num1 = 0, $num2 = 1.
  2. Print $num1 and $num2 as these are the starting numbers
  3. In while loop, 10 is the limit
  4. Add $num1 and $num2 and store to $num3
  5. Print $num3 as it is the next number
  6. Now we have to do the swapping, that means $num1 assigned with value of $num2, $num3 is assigned to $num2.
  7. Then $num incremented by 1

For more details about Fibonacci series click here

For other PHP Programs please click here

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top