Fibonacci series is just one of the exercises given by most programming instructors. Some other exercises are the following:
- Create a simple php calculator
- Celsius to Fahrenheit Temperature Converter
- Identify if odd or even number
- Display numbers using a for loop
- Solve for the area of a triangle
and many more. These are just some of the activities that I posted here in my blog. You can visit the links if you want.
Fibonacci series is achieved by adding the first and second value to solve for the third number. And then the 2nd and third to solve for the 4th number and so on and forth. The series starts with 0 and 1 and then 1, 3, 5, 8, 13 and so on. To be able to achieve this, we need to use to looping statement. In my example, I used the while loop.
The following code will display a fibonacci series and will stop until the 10th number. Your $i there will serve as a counter and your $stopper is where the loop will stop.
PHP Code Fibonacci Series
<?phpTry Fibonacci Series Program in PHP here live!
$a=0;
$b=1;
$stop=10;
$i=1;
echo $b."<br>";
while ($i<$stop)
{
$sum=$a+$b;
echo $sum;
echo "<br>";
$i++;
$a=$b;
$b=$sum;
}
?>
No comments:
Post a Comment