Saturday, 3 August 2013

How to make a Fibonacci series using PHP

In this article i will write a program to print Fibonacci numbers upto 20. This is very basic program we were writing in our college time. In Fibonacci number each number is sum of previous tow numbers. first tow numbers of Fibonacci number is 0 and 1, the next number will be (0+1) = 1, next will be (1+1) = 2, next will be (1+2) = 3 and so on.
Here is the code for Fibonacci numbers.

<?php
     $limit = 20;
     echo $first = 0;echo '<br/>';
     $second = 1;
     while($limit > 0){
          echo $next = $first + $second;echo '<br/>';
          $second = $first;
          $first = $next;
          $limit--;
     }
?>
Here is the Output :

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

No comments:

Post a Comment