Example 51: The Fibonacci Sequence

Learn how to solve the Fibonacci sequence problem using recursion.

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.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.