Q:

Fibonacci Series Program in PHP

belongs to collection: PHP Programming Exercises

0

Fibonacci Series Program in PHP

In this exercise, you will learn different ways to write Fibonacci series of programs using PHP.

The Fibonacci series are the sequence of numbers in which the next number is the sum of the previous two numbers. The Fibonacci series was well-known hundreds of years earlier. The "Fibonacci" name came from the nickname "Bonacci".

We can easily remember Fibonacci Sequence using the Fibonacci Day, which is November 23rd. As 23rd November has the digits "1, 1, 2, 3" which is part of the sequence.

0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5

0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34....

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Fibonacci Series Program in PHP using For Loop

Here, you will learn how to print Fibonacci series using a for loop in PHP. In this below code, $f1 contains first number, i.e., 0 and $f2 contains second number, i.e., 1 and $n contains total Fibonacci series number count.

<?php
    $f1 = 0;
    $f2 = 1;
    $n = 30;
    echo $f1;
    echo '<br/>';
    echo $f2;
    for($i = 1; $i <= $n; $i++) {
        $f3 = $f1 + $f2;
        $f1 = $f2;
        $f2 = $f3;
        echo $f3 ."<br />"; 
    }
?>

Output of the above code
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269

Fibonacci Series Program in PHP using while loop

In the given program, we have used the while loop to print the Fibonacci series upto 15 in PHP.

<?php  
$n = 0;  
$a = 0;  
$b = 2;  
echo "Fibonacci series upto 15 : ";   
echo "$a, $b";  
 
while ($n < 16 )   
{  
  $c = $b + $a;  
  echo ", ";
  echo "$c";
  $a = $b;  
  $b = $c;  
  $n = $n + 1;
}
?> 

Output of the above code: 

Fibonacci series upto 15 : 0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178, 288, 466, 754, 1220, 1974, 3194 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to generate QR Code in PHP... >>
<< How to check whether a year is a leap year or not ...