![]() |
VOOZH | about |
Find the first 10 numbers of Fibonacci series.
The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ........
Approach: To solve the problem, follow the below idea:
We can take two variables f1 and f2 to store the first and second term. In order to calculate the next terms, we can take the sum of previous two terms f1 and f2 and then update f1 with f2 and f2 with the new term. Repeat for 10 times to get first 10 numbers of Fibonacci Sequence.
Step-by-step algorithm:
Below is the implementation of the approach:
0 1 1 2 3 5 8 13 21 34
Time Complexity: O(10) ~ O(1)
Auxiliary Space: O(1)