Open In App

JavaScript Program to Display Fibonacci Sequence Using Recursion

Last Updated : 21 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 itself.

A Fibonacci series: The Fibonacci Sequence is a series of numbers in which each number is the sum of the two preceding ones, starting with 0 and 1.

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

The Fibonacci sequence using a recursive formula:

F(n) = F(n-1) + F(n-2)

where F(n) is the nth number in the sequence, F(n-1) is the (n-1)th number, and F(n-2) is the (n-2)th number. The sequence starts with F(0) = 0 and F(1) = 1.

Examples: Let’s dive into the implementation and understand how we can achieve this using recursion.

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

The Fibonacci function takes a number n as input and returns the Fibonacci number at the position n in the sequence. The base case for the recursion is when n is less than or equal to 1. In this case, we simply return n itself, as the Fibonacci sequence starts with 0 and 1. For any other value ofn, we recursively call the fibonacci function with n - 1 and n - 2 as arguments. as it calculates the sum of the two preceding numbers in the sequence.

Syntax:

function fibonacci(n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

Approach

In this approach, The provided JavaScript function recursively calculates the first 10 numbers in the Fibonacci sequence and prints them.

Fibonacci Sequence for n=10

Example 1: In this example, we are using the above-explained approach.

Javascript




function fibonacci(n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}
  
// Printing n fibonacci sequence
n = 10
  
for (let i = 0; i < n; i++) {
    console.log(fibonacci(i));
};


Output

0
1
1
2
3
5
8
13
21
34


Example 2: In this example, we are using the recursive function to calculate the Fibonacci sequence and display the sequence up to the user-inputted number.

Javascript




function fibonacci(n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}
  
function displayFibonacciSequence(n) {
    let result = '';
    for (let i = 0; i < n; i++) {
        result += fibonacci(i) + ' ';
    }
    console.log(result);
}
  
// Take user input from the console
const n = parseInt(prompt('Enter a number:'));
if (!isNaN(n) && n >= 0) {
    displayFibonacciSequence(n);
} else {
    alert('Invalid input. Please enter a Positive integer.');
};


Output:

Fibonacci.gif



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads