Obtain sum of Fibonacci series without using function

Fibonacci series is a such kind of series of numbers in which the current number is the addition of previous two numbers. It is a mathematical series. Today we will see How to make Fibonacci series by PHP programming without using array or function

Procedure:

  1. We will collect the 1st number, 2 number and the length of series by HTML.
  2. Then we will print them serially.
  3. Add them. It will be the third number.
  4. Print the third number and generate the next number. Add this number with the previous sum.
  5. Use a loop till the length ends. It will generate the Fibonacci Sequence.
  6. Print them.

First generate the HTML to read data.

<form action='?' method='POST'>
Enter the startig number: <input name="start" /><br/>
Enter the second number: <input name="second" /><br/>
Enter the series length: <input name="length" /><br/>
<input type='submit' value='Generate Fibonacci'/>
</form>

Now do the operations.

<?php 
if(isset($_POST['start']))
{
 $start = $_POST['start'];
 $length = $_POST['length'];
 $second = $_POST['second'];
 print "<u><b>The fibonacci series is:</b></u> " . $start ."+" . $second;
 $now = $start + $second;
 $sum = $now;
 for($i=3;$i<=$length;$i++)
 {
  print "+" . $now;
  $cache = $now;
  $sum = $sum + $now;
  $now = $now + $second;
  $second = $cache;
 }
 print "=$sum";
 
}
?>

Note: This procedure can be used in another programming languages. You have to just change the commands and operators their.

Happy programming!


Related Posts
Previous
« Prev Post