Friday, 12 August 2016

PHP Basics : Fibonacci Series

Hi, today lets see about fibonacci series using php

You guys know about fibonacci series, but once again i will explain you about fibonacci before entering the code

First two fibonacci number will be 0 and 1 and the series that follow will be the sum of two previous numbers

so our series will look like

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144


Lets begin your code





<?php

// Declare three variables 

$add = 0 ;
$first_fibonacci_number = 0;
$second_fibonacci_number = 1;
echo "$first_fibonacci_number, $second_fibonacci_number, ";

//Create a while loop to echo out the fibonacci series, Here lets print out first 30 fibonacci series
// followed by 0 and 1 

while ($add < 30 )
{
$next_fibonacci_number = $second_fibonacci_number + $first_fibonacci_number ;
echo "$next_fibonacci_number, ";
$first_fibonacci_number = $second_fibonacci_number ;
$second_fibonacci_number = $next_fibonacci_number ;
$add = $add + 1;
}

?>

You can run in browser using localhost/folder_name/filename.php (if you installed xampp)


Its cool right ?


No comments:

Post a Comment