Tech Study

Write a PHP program to find the Fibonacci series

Introduction

Write a PHP program to find the Fibonacci series

What is the Fibonacci series?

The Fibonacci series is the one in which you will get your next number by adding the previous 2 numbers.

Method No 1: Recursive way

<?php  
// Recursive function 
function Fibonacci($number){
       if ($number == 0)
        return 0;    
    else if ($number == 1)
        return 1;    
      
    // Recursive Call to get numbers
    else
        return (Fibonacci($number-1) + 
                Fibonacci($number-2));
}
  
$number = 10;
for ($counter = 0; $counter < $number; $counter++){  
    echo Fibonacci($counter),' ';
}
?>

Method No 2: Iterative way

<?php
function Fibonacci($n){
  
    $num1 = 0;
    $num2 = 1;
  
    $counter = 0;
    while ($counter < $n){
        echo ' '.$num1;
        $num3 = $num2 + $num1;
        $num1 = $num2;
        $num2 = $num3;
        $counter = $counter + 1;
    }
}
  
$n = 10;
Fibonacci($n);
?>

Result

Write a PHP program to find the Fibonacci series
Write a PHP program to find the Fibonacci series

TaggedWrite a PHP program to find the Fibonacci series

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more