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
<?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