View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Understanding Fibonacci Series in C

Updated on 17/04/2025330 Views

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!

What is the Fibonacci Series in C?

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:

  • F0 = 0

  • F1 = 1

  • F2 = 1

  • F3 = 2

  • F4 = 3

  • F5 = 5

  • F6 = 8

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!

Fibonacci Series in C Example Table

n

0

1

2

3

4

5

6

7

8

9

Fn

0

1

1

2

3

5

8

13

21

34

How to Implement Fibonacci Series in C?

The Fibonacci Series in C can be implemented in three main ways:

  • Using a Loop (Iterative Method)
  • Using Recursion
  • Using an Array (Dynamic Programming Approach)

Let's explore each method with code examples and explanations.

Must Read: 29 C Programming Projects in 2025 for All Levels [Source Code Included]

Fibonacci Series using Loop in C

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:

  • The program starts by printing the first two numbers, 0 and 1.
  • In each iteration, it calculates the next Fibonacci number by adding the last two numbers.
  • It uses a loop to repeat this process for the given number of terms.

Also Explore: What are Data Structures in C & How to Use Them?

Fibonacci Series using Recursion in C

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:

  • The function fibonacci() calls itself to calculate each number in the series.
  • It uses base cases (n = 0 and n = 1) to stop the recursion.
  • Each Fibonacci number is calculated by summing the previous two numbers.

Note: Recursion can be inefficient because it repeatedly recalculates the same values, leading to a deep call stack.

Must Explore: Introduction to C Tutorial

Fibonacci Series using Array in C

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:

  • The program stores each Fibonacci number in an array.
  • Each number is calculated using the sum of the last two stored values.
  • This approach is faster than recursion and avoids redundant calculations.

Time and Space Complexity of Fibonacci Implementations in C

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: 

  • Loop is the fastest and uses the least memory.
  • Recursion is slow due to repeated calculations and deep recursion.
  • Array is fast and avoids redundant work but uses more memory.

Fibonacci Series in C Case Studies 

Case Study 1: Savings Prediction

Scenario: A student wants to predict savings for 12 months. They follow a Fibonacci-like plan:

  • Month 1: Save INR 0 
  • Month 2: Save INR 1
  • From Month 3: Save the sum of the previous two months

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!

Case Study 2: Predicting Viral Growth of Social Media Posts

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:

  • Day 1: 1 share
  • Day 2: 1 new share
  • From Day 3: Shares = total of shares from the previous two days

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

Case Study 3: Plant Growth with Reproduction Delay

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.

Conclusion

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.

FAQ

1. What is the Fibonacci Series in C?

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.

2. How do you write the Fibonacci Series using a loop 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.

3. How is recursion used to generate the Fibonacci Series in C?

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.

4. What is the advantage of using arrays to generate Fibonacci numbers in C?

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).

5. Which Fibonacci method in C is the fastest and most efficient?

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.

6. How does the Fibonacci Series appear in real-life case studies?

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.

7. Can Fibonacci logic help in financial planning or savings?

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.

8. How can the Fibonacci Series model viral social media growth?

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.

9. What is the plant reproduction delay model based on Fibonacci?

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.

10. What are the time and space complexities of different Fibonacci methods in C?

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.

11. What is the difference between iteration and recursion for Fibonacci?

Iteration uses loops to calculate Fibonacci numbers, while recursion uses function calls. Iteration is faster, while recursion is more intuitive but slower.

12. Why does the loop method have O(n) space complexity?

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.

image

Take a Free C Programming Quiz

Answer quick questions and assess your C programming knowledge

right-top-arrow
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
advertise-arrow

Free Courses

Start Learning For Free

Explore Our Free Software Tutorials and Elevate your Career.

upGrad Learner Support

Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918068792934

Disclaimer

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.