Example 51: The Fibonacci Sequence
Learn how to solve the Fibonacci sequence problem using recursion.
We'll cover the following
Problem
The Fibonacci sequence starts with 0 and 1. After the first two terms, each number in the Fibonacci sequence is the sum of its two previous numbers.
Write a recursive function to generate the Fibonacci sequence.
Example
Input | Output |
---|---|
5 | 0 1 1 2 3 |
10 | 0 1 1 2 3 5 8 13 21 34 |
15 | 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
Try it yourself
Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.
Get hands-on with 1400+ tech skills courses.