![]() |
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 The Fibonacci function takes a number Syntax: function fibonacci(n) { ApproachIn 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.
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.
Output: ![]() |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |