JavaScript Program to Display Fibonacci Sequence Using Recursion
Last Updated :
29 Jul, 2024
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));
};
Output0
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:
Similar Reads
JavaScript Program to Print 1 to N using Recursion In this article, we will see how to print 1 to N using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
2 min read
JavaScript Program to Print N to 1 using Recursion In this article, we will see how to print N to 1 using Recursion in JavaScript. What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. In the recursive program, the solution to the base
1 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 met
2 min read
Python Program to Display Fibonacci Sequence Using Recursion We are given a task to write the Fibonacci sequence using recursion. we will take the range as input of integer and then print the Fibonacci Sequence. In this article, we will see the method of Python Program to Display Fibonacci Sequence Using Recursion. Example: Input: n = 9Output: 0 1 1 2 3 5 8 1
2 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 recurrence relation defines the sequence Fn of Fibonacci numbers:Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1Examples:Input : 5
4 min read