For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
The Fibonacci Series in C is a classic coding problem that is often used to teach and test programming concepts. It starts with the numbers 0 and 1. Each next number is the sum of the previous two numbers. This simple rule makes it an excellent choice for understanding loops, recursion, and arrays in C programming.
The Fibonacci sequence is not only important in coding but also appears in nature, finance, and various real-world scenarios. Let's dive into how to implement the Fibonacci Series in C using different methods and gain a deeper understanding of this problem.
Must explore top online software engineering courses in 2025!
In C programming, the Fibonacci Series is a sequence where each number is the sum of the two preceding ones. The series starts with the numbers 0 and 1, and the sequence can be defined as:
Fn = Fn - 1 + Fn - 2
This means:
and so on.
The Fibonacci Series is a sequence of numbers commonly used in programming exercises to explore recursion, iteration, and data storage techniques.
Unlock your potential with online DBA programs or enroll in the first-of-its-kind Generative AI Doctorate program from Golden Gate University!
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Fn | 0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 |
The Fibonacci Series in C can be implemented in three main ways:
Let's explore each method with code examples and explanations.
Must Read: 29 C Programming Projects in 2025 for All Levels [Source Code Included]
The loop method is the simplest and most efficient way to generate Fibonacci numbers.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
Output:
Enter the number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Explanation:
Also Explore: What are Data Structures in C & How to Use Them?
The recursive method involves defining a function that calls itself to calculate each Fibonacci number. If you want to gain in-depth knowledge about recursion, read the recursion in C article.
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
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;
}
Output:
Enter the number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Explanation:
Note: Recursion can be inefficient because it repeatedly recalculates the same values, leading to a deep call stack.
Must Explore: Introduction to C Tutorial
The array method uses an array to store previously calculated Fibonacci numbers, which avoids redundant calculations and speeds up the process. If you want to gain in-depth knowledge about arrays, read the what is Array in C article .
#include <stdio.h>
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
return 0;
}
Output:
Enter the number of terms: 8
Fibonacci Series: 0 1 1 2 3 5 8 13
Explanation:
Here is the time and space complexity of fibonacci implementations in C programming language:
Method | Time | Space |
Loop | O(n) | O(1) |
Recursion | O(2^n) | O(n) |
Array (DP) | O(n) | O(n) |
Note:
Scenario: A student wants to predict savings for 12 months. They follow a Fibonacci-like plan:
Code:
#include <stdio.h>
int main() {
int months = 12;
int savings[months];
savings[0] = 0;
savings[1] = 1;
for (int i = 2; i < months; i++) {
savings[i] = savings[i - 1] + savings[i - 2];
}
printf("Monthly Savings (in ₹): ");
for (int i = 0; i < months; i++) {
printf("%d ", savings[i]);
}
return 0;
}
Output:
Monthly Savings (in INR): 0 1 1 2 3 5 8 13 21 34 55 89
Tip: Use arrays to track monthly data. It scales easily and avoids repeat work.
Check out the Executive Diploma in Data Science & AI with IIIT-B!
Scenario: A digital marketing intern is analyzing how a post spreads on social media. Based on past trends, the number of shares follows a pattern like this:
They decide to use the Fibonacci Series to predict reach over 10 days.
Code:
#include <stdio.h>
int main() {
int days = 10;
int shares[days];
shares[0] = 1;
shares[1] = 1;
for (int i = 2; i < days; i++) {
shares[i] = shares[i - 1] + shares[i - 2];
}
printf("Estimated Shares over 10 Days: ");
for (int i = 0; i < days; i++) {
printf("%d ", shares[i]);
}
return 0;
}
Output:
Estimated Shares over 10 Days: 1 1 2 3 5 8 13 21 34 55
Tip: You can scale the model by adding a multiplier (e.g., 2x for retweets or shares per person) to predict higher-order viral trends
Scenario: You are given a plant species where each plant takes 2 full cycles to mature. After that, it produces one new plant every cycle. Model this growth for 10 cycles using the Fibonacci logic with a delay.
Approach: We use a modified Fibonacci pattern. The first two terms are fixed as 1. From the third term onward, each term is the sum of the two previous terms, representing delayed reproduction.
Code:
#include <stdio.h>
int main() {
int n = 10;
int plants[n];
plants[0] = 1; // Cycle 1
plants[1] = 1; // Cycle 2
for (int i = 2; i < n; i++) {
plants[i] = plants[i - 1] + plants[i - 2]; // Reproduction starts after 2 cycles
}
printf("Plant count after each cycle: ");
for (int i = 0; i < n; i++) {
printf("%d ", plants[i]);
}
return 0;
}
Output:
Plant count after each cycle: 1 1 2 3 5 8 13 21 34 55
Explanation: Only mature plants (2+ cycles old) can reproduce. This gives a Fibonacci-like sequence and mirrors realistic growth cycles in nature.
The Fibonacci Series in C helps you understand key programming concepts such as loops, recursion, and dynamic programming. The iterative approach is the fastest, recursion is useful for practice, and dynamic programming is ideal for reducing redundant calculations.
The Fibonacci Series in C is a sequence that starts with 0 and 1. Every next number is the sum of the previous two. It’s a common programming concept used to understand loops, recursion, and arrays effectively in C.
You use a simple for loop starting with 0 and 1. Each iteration adds the last two numbers to get the next one. It’s the most efficient method with a time complexity of O(n) and constant memory use.
Recursion in C uses a function that calls itself to calculate Fibonacci numbers. The base cases are 0 and 1. This method is elegant but inefficient, as it recalculates many values and consumes more memory due to the deep call stack.
Using arrays helps store each Fibonacci number as you calculate it. This avoids repeated calculations, making the process faster. The dynamic programming approach improves efficiency but uses more memory, with both time and space complexity as O(n).
The loop method is the fastest and most memory-efficient. It computes each number iteratively without storing all previous values. This approach has O(n) time complexity and O(1) space complexity, making it ideal for large sequences in C.
The Fibonacci Series appears in various fields like savings predictions, viral content growth, and plant reproduction. These cases use the same logic of growth based on previous values, showcasing its real-world applications beyond programming.
Yes, Fibonacci logic can model savings patterns where each month’s saving is the sum of the last two. This approach helps predict and plan financial growth simply and visually, especially when implemented using arrays in C.
Fibonacci logic tracks how shares multiply daily based on past performance. By summing up shares from previous days, marketers can estimate how fast a post might go viral. This predictive model helps in planning digital campaigns.
In this model, each plant takes two cycles to mature. From the third cycle, each mature plant produces one new plant. This delayed growth closely follows the Fibonacci pattern and simulates real-world biological cycles efficiently.
The loop method has O(n) time and O(1) space complexity. Recursion has exponential time (O(2^n)) and O(n) space. Using arrays gives O(n) time and space complexity. Each method suits different scenarios based on performance and memory needs.
Iteration uses loops to calculate Fibonacci numbers, while recursion uses function calls. Iteration is faster, while recursion is more intuitive but slower.
The loop method has O(1) space complexity because it uses only a few variables (two for the previous two Fibonacci numbers), whereas the array method uses O(n) space to store the Fibonacci series.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.