The Fibonacci numbers start with F(0) = 0 and F(1) = 1, and after that every number is the sum of the two immediately before it: F(n) = F(n - 1) + F(n - 2) for n greater than 1.
Given an index n, return the value F(n).
Example 1
Input: n = 2
Output: 1
F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2
Input: n = 5
Output: 5
The sequence up to index 5 is 0, 1, 1, 2, 3, 5, so F(5) = 5.
Constraints
0 ≤ n ≤ 30The result fits inside a signed 32-bit integer.See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI