Open In App

JavaScript Program to print Fibonacci Series

Last Updated : 14 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 : 8
Input :8
Output :34

There are three methods to print the Fibonacci series, which are described below:

Approach 1: Using For loop

Example: This example uses for loop to print the Fibonacci series.

javascript
function fibonacci(num) {
    let num1 = 0;
    let num2 = 1;
    let sum;
    if (num === 1) {
        return num1;
    } else if (num === 2) {
        return num2;
    } else {
        for (let i = 3; i <= num; i++) {
            sum = num1 + num2;
            num1 = num2;
            num2 = sum;
        }
        return num2;
    }
}

console.log("Fibonacci(5): " + fibonacci(5));
console.log("Fibonacci(8): " + fibonacci(8));

Output:

Fibonacci(5): 3
Fibonacci(8): 13

Approach 2: Using While loop

Example: This example uses a while loop to print the Fibonacci series.

javascript
function fibonacci(num) {
    if (num == 1)
        return 0;
    if (num == 2)
        return 1;
    let num1 = 0;
    let num2 = 1;
    let sum;
    let i = 2;
    while (i < num) {
        sum = num1 + num2;
        num1 = num2;
        num2 = sum;
        i += 1;
    }
    return num2;
}

console.log("Fibonacci(5): " + fibonacci(15));
console.log("Fibonacci(8): " + fibonacci(18));

Output:

Fibonacci(5): 3
Fibonacci(8): 13

Approach 3: Using Recursion

As we know that the nth Fibonacci number is the summation of n-1 and n-2 term and the n-1 term is the summation of n-2 and n-3 terms. So, to get the nth Fibonacci term we can follow fib(n)=fib(n-1)+fib(n-2) fib(n)=fib(n-2)+fib(n-3)+fib(n-3)+fib(n-4) …. fib(n)=fib(1)+fib(0)+fib(1)+fib(0)+fib(1)+fib(0)….fib(1)+fib(0) [terms containing sum of fib(1) and fib(0) fib(1)=0 fib(2)=1

Example: This example shows the use of the recursion approach.

javascript
function fibonacci(num) {
    if (num == 1)
        return 0;
    if (num == 2)
        return 1;
    return fibonacci(num - 1) + fibonacci(num - 2);
}
console.log("Fibonacci(5): " + fibonacci(5));
console.log("Fibonacci(8): " + fibonacci(8));

Output:

Fibonacci(5): 3
Fibonacci(8): 13

Approach 3: Using Generator Function:

Using a generator function, Fibonacci numbers are yielded incrementally. It maintains two variables representing the current and previous Fibonacci numbers. With each iteration, the next Fibonacci number is calculated and yielded, allowing for efficient memory usage and on-demand generation of the series.

Example:

JavaScript
function* fibonacciGenerator(num) {
    let num1 = 0;
    let num2 = 1;
    yield num1;
    yield num2;
    for (let i = 3; i <= num; i++) {
        let sum = num1 + num2;
        num1 = num2;
        num2 = sum;
        yield num2;
    }
}

function fibonacci(num) {
    let result;
    const iterator = fibonacciGenerator(num);
    for (let i = 0; i < num; i++) {
        result = iterator.next().value;
    }
    return result;
}

console.log("Fibonacci(5): " + fibonacci(5));
console.log("Fibonacci(8): " + fibonacci(8));

Output
Fibonacci(5): 3
Fibonacci(8): 13




Previous Article
Next Article

Similar Reads

JavaScript Program to Print Nth Non Fibonacci Number
JavaScript program to print the nth non-Fibonacci number. Non-Fibonacci numbers are integers that are not part of the Fibonacci sequence. Below are the approaches to print the Nth non-Fibonacci number: Table of Content Using a LoopUsing RecursionUsing a LoopWe are using a while loop to find the nth non-Fibonacci number. we iterate through Fibonacci
2 min read
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
PHP | Fibonacci Series
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 Outp
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
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
JavaScript Program to Sum of Fibonacci Numbers at Even Indexes up to N Terms
Fibonacci numbers are a sequence of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. In this article, we will explore how to calculate the sum of Fibonacci numbers at even indexes up to N terms using JavaScript. Sum of Fibonacci Numbers at Even Indexes up to N Terms using IterationThe basic method is to use a l
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
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
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