The Fibonacci Series is one of the most common and fundamental concepts in Data Structures and Algorithms (DSA).
It forms the basis for understanding recursion, dynamic programming, and algorithm optimization.
🔢 What is the Fibonacci Series?
The Fibonacci series is a sequence of numbers where:
F(n) = F(n-1) + F(n-2)with the first two numbers defined as:
F(0) = 0, F(1) = 1So, the series looks like:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Each number is the sum of the two preceding numbers.
💡 Real-World Applications
You may be surprised, but the Fibonacci sequence appears in many real-world scenarios, such as:
🌻 Nature: Patterns in flowers and leaves
💰 Finance: Predicting stock market movements
🧬 Biology: DNA and population growth models
💻 Computer Science: Algorithm design and dynamic programming problems
🧠 Approach 1. Iterative Method
The iterative approach is more efficient and simple to implement.
✅ Algorithm Steps
Initialize first two numbers as
0and1.Loop from 2 to
n.Calculate the next number as the sum of the previous two.
Print each number as you go.
💻 C Program Example
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for(i = 0; i < n; i++) {
if(i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
⏱ Time Complexity: O(n)
💾 Space Complexity: O(1)
🔁 Approach 2. Recursive Method
The recursive approach follows the direct mathematical definition of Fibonacci numbers.
However, it is less efficient because it performs repeated calculations.
💻 C Program Example
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for(int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Join the conversation! Your thoughts help the community grow.