Open In App

PHP | Fibonacci Series

Last Updated : 13 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The Fibonacci series is a series of elements where, the previous two elements are added to get the next element, starting with 0 and 1. In this article, we will learn about how to generate a Fibonacci series in PHP using iterative and recursive way. Given a number n, we need to find the Fibonacci series up to the nth term.
Examples:

Input : 10
Output : 0 1  1 2 3 5 8 13 21 34

Input : 15
Output : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 

Method 1: Using Recursive way
Recursion is a way where we repeatedly call the same function until a base condition is matched to end the recursion.




<?php  
// PHP code to get the Fibonacci series
// Recursive function for fibonacci series.
function Fibonacci($number){
      
    // if and else if to generate first two numbers
    if ($number == 0)
        return 0;    
    else if ($number == 1)
        return 1;    
      
    // Recursive Call to get the upcoming numbers
    else
        return (Fibonacci($number-1) + 
                Fibonacci($number-2));
}
  
// Driver Code
$number = 10;
for ($counter = 0; $counter < $number; $counter++){  
    echo Fibonacci($counter),' ';
}
?>


Output:

0 1  1 2 3 5 8 13 21 34

Method 2: Using the iterative way
At first, we initialize the first and second number to 0 and 1. Following this, we print the first and second number. Then we send the flow to the iterative while loop where we get the next number by adding the previous two number and simultaneously we swap the first number with the second and the second with the third.




<?php
// PHP code to get the Fibonacci series
function Fibonacci($n){
  
    $num1 = 0;
    $num2 = 1;
  
    $counter = 0;
    while ($counter < $n){
        echo ' '.$num1;
        $num3 = $num2 + $num1;
        $num1 = $num2;
        $num2 = $num3;
        $counter = $counter + 1;
    }
}
  
// Driver Code
$n = 10;
Fibonacci($n);
?>


Output:

0 1  1 2 3 5 8 13 21 34


Similar Reads

nth Multiple of a Number in Fibonacci Series in JavaScript
The Fibonacci series is a sequence of numbers where each number is the sum of the two previous ones, usually starting with 0 and 1. Given two integers n and k, the task is to find the position of the nth multiple of k in the Fibonacci series. For instance, if k = 2 and n = 3, the output would be 9 since the third multiple of 2 in the Fibonacci seri
4 min read
How to calculate the Fibonacci series in JavaScript ?
Fibonacci series is a number series that contains integers in the following pattern. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ..In terms of mathematics, the general formula for calculating the Fibonacci series is fn = fn-1 + fn-2 , where n ≥ 2Here, f0 = 0 and f1 = 1. We need to calculate n Fibonacci numbers for any given integer n, where n ≥ 0. Example
4 min read
JavaScript Program to print Fibonacci Series
The Fibonacci sequence is the integer sequence where the first two terms are 0 and 1. After that, the next term is defined as the sum of the previous two terms. The sequence Fn of Fibonacci numbers is defined by the recurrence relation: Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1 Examples: Input : 5 Output : 8Input :8Output :34There are thr
3 min read
PHP Program for Nth Fibonacci Number
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. In this article, we will write a PHP program to find the Nth Fibonacci number. Table of Content Iterative MethodRecursive MethodIterative MethodFirst, we define a function that takes an integer $n as a parameter. Insi
2 min read
Check if a Given Number is Fibonacci Number in PHP
Given a number N, the task is to check whether the given number is a Fibonacci number or not. Examples: Input: 21Output: Fibonacci NumberInput: 35Output: Not a Fibonacci NumberWhat is the Fibonacci Series?The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers of the Fibon
2 min read
PHP program to print an arithmetic progression series using inbuilt functions
We have to print an arithmetic progressive series in PHP, between two given numbers a and b both including, a given common arithmetic difference of d. Examples: Input : $a = 200, $b = 250, $d = 10 Output : 200, 210, 220, 230, 240, 250 Input : $a = 10, $b = 100, $d = 20 Output : 10, 30, 50, 70, 90This problem can be solved using loops by iterating f
2 min read
PHP Program to Find Sum of Geometric Series
Given a number n, the task is to find the sum of the geometric series in PHP. A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. PHP Program to Sum of Geometric Series using for LoopA Simple solution to calculate the sum of geometric serie
2 min read
JavaScript Program to Display Fibonacci Sequence Using Recursion
In this article, we will explore how to display the Fibonacci sequence using recursion in JavaScript. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. Recursion is a powerful technique in programming that involves a function calling itself. A recursive function refers to a function that calls its
3 min read
How to Check if a Given Number is Fibonacci Number in JavaScript ?
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Checking for Fibonacci numbers involves verifying whether a given number appears in this sequence. The first few Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and 89, and so on. There are various approaches to ch
4 min read
JavaScript Program to Find n-th Fibonacci Number
The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. Given an integer n, the task is to find the n-th Fibonacci number. These are the following methods: Table of Content Recursive ApproachIterative
2 min read