Friday, April 27, 2018

Fibonacci Series in php

  This program print the fibonacci series.

<?php
//0 1 1 2 3 5 8 13 21 34
  class Fib {
     function printFib($n) {
        if($n==0) return 0;
        $a=0;$b=1;$c;
        echo $a." ".$b ;
        for($i=2;$i<=$n;$i++) {
        $c=$a+$b;
        $a=$b;
        $b=$c;
        echo $c." ";         
        }
     }
  }
 $fib=new Fib();
 $fib->printFib(5);
 ?>

output:

0 11 2 3 5

No comments:

Post a Comment

Featured Post

What is JavaScript? What is the role of JavaScript engine?

  The JavaScript is a Programming language that is used for converting static web pages to interactive and dynamic web pages. A JavaScript e...