Right arrowSoftware Development

Fibonacci Series in C: Program, Logic, and Examples

author image

Bosscoder Academy

Date: 6th June, 2026

feature image

The Fibonacci Series is one of the most frequently asked programming concepts in coding job interviews for beginner and working professionals. It is also important to know how to calculate the Fibonacci Series when you are learning C programming as a first time programmer or if you are reviewing fundamental programming concepts prior to your technical interview.

This blog will describe the Fibonacci Series using an example to help you understand the logic associated with it, provide examples of different implementations of the Fibonacci Series in C, and show you how the Fibonacci Series can be used practically.

What is the Fibonacci Series?

The Fibonacci Series is a sequence of numbers where each number is the sum of the two preceding numbers.

The Fibonacci Series looks something like this:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The formula is:

F(n) = F(n-1) + F(n-2)

Where:

→ F(0) = 0
→ F(1) = 1

For example:

0 + 1 = 1
1 + 1 = 2
1 + 2 = 3
2 + 3 = 5

Importance of the Fibonacci Series

The Fibonacci Series is frequently included in:

→ Coding practice in C
→ Coding style during coding interviews
→ Data Structures and Algorithms (DSA)
→ Understanding of recursion
→ Dynamic Programming problems
→ Mathematical modeling

Many top tech companies ask Fibonacci-based questions that test a candidate's understanding with loops, recursion, or how they can be optimized to improve efficiency.

Bosscoder Academy CTA

Logic of Fibonacci Series in C

The logic is straightforward:

  1. Initialize the first two numbers as 0 and 1.
  2. Print them.
  3. Next you will find the third term by summing the last two values.
  4. Update the values.
  5. Repeat until the required number of terms is generated.

Visualization:

First Number  = 0
Second Number = 1

Next Number = 0 + 1 = 1
Next Number = 1 + 1 = 2
Next Number = 1 + 2 = 3
Next Number = 2 + 3 = 5

Fibonacci Series Program in C Using Loop

This is the most commonly used approach.

Code:

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;

    printf("Enter 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 number of terms: 10

Fibonacci Series:
0 1 1 2 3 5 8 13 21 34

Fibonacci Series in C Using Recursion

Recursion is another popular method for generating Fibonacci numbers.

Code:

#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 number of terms: ");
    scanf("%d", &n);

    for(int i = 0; i < n; i++) {
        printf("%d ", fibonacci(i));
    }

    return 0;
}

Output:

0 1 1 2 3 5 8 13 21 34

Limitation of Recursion

Recursion can be an easy way to solve the series, but it becomes a slower solution the larger the desired number of terms to be generated, since you constantly calculate repeated values.

When preparing for interviews candidates should also have knowledge of other optimized techniques such as Dynamic Programming.

Find the Nth Fibonacci Number in C

Sometimes interviewers ask for only the nth Fibonacci number instead of the complete series.

Code:

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;

    printf("Enter n: ");
    scanf("%d", &n);

    if(n == 0) {
        printf("0");
        return 0;
    }

    for(int i = 2; i <= n; i++) {
        next = first + second;
        first = second;
        second = next;
    }

    printf("%d", second);

    return 0;
}

Example

Input:

7

Output:

13

The 7th Fibonacci number is 13.

Time Complexity of Fibonacci Approaches

Approach Time Complexity Space Complexity
Loop (Iterative) O(n) O(1)
Recursion O(2ⁿ) O(n)
Dynamic Programming O(n) O(n)
Optimized DP O(n) O(1)

The iterative approach is usually the best choice for all interviews and all types of jobs, as it is the most efficient way to write code.

Commonly Asked Interview Questions on the Fibonacci Series:

There are several examples of variations of the Fibonacci problems that are commonly asked by interviewers. For example:

- How to print the Fibonacci sequence to 'n' terms
- How to find the nth number in the Fibonacci Sequence
- How to generate a Fibonacci sequence using Recursion
- How to determine if a number is part of the Fibonacci Sequence
- How to optimize the performance of the Fibonacci Algorithm using Dynamic Programming

If you understand the core logic behind the Fibonacci numbers, then you can solve all of the above mentioned variations with ease!

How Working Professionals Can Master Such Programming Concepts

Many software engineers find it difficult to transition between jobs due to a lack of practice with basic programming concepts over time. Technical interview questions involving loops, recursion, arrays, and data structures & algorithms are common in job interviews with product-based companies.

Programs like those offered by Bosscoder Academy help working professionals strengthen their foundations in Data Structures, Algorithms, System Design, and problem-solving through structured learning, 1:1 mentorship, and interview-focused preparation.

Revisiting concepts like the Fibonacci Series is often the first step toward improving coding confidence and interview performance.

Final Thoughts

Learning how to implement Fibonacci Series in C is one of the most important programming practices to help you understand loops, recursion, and thinking through an algorithm. You will see this question in most coding interviews, and it is a foundation for more advanced concepts such as Dynamic Programming.

Again, whether you are just getting started learning C or are a working professional getting ready for a technical interview, becoming proficient at writing Fibonacci programs will help you hone your programming skills and algorithmic abilities.

Make sure to practice both the iterative and recursive approaches, have an understanding of their time and space complexities, and work on related interview questions to help strengthen your programming skill sets.

Frequently Asked Questions (FAQs)

Q1. How do you write a Fibonacci Series program in C?

A program can be created utilizing either loops or recursion to generate a Fibonacci series. The iterative approach is preferred by many programmers as it is generally more efficient and requires less memory than recursion.

Q2. What is the logic behind the Fibonacci Series?

The reasoning behind using Fibonacci is fairly easy to understand: The current number in the sequence is derived from the sum of the previous two numbers.
F(n) = F(n - 1) + F(n - 2)

Q3. Which method is better for Fibonacci Series in C: loop or recursion?

The implementation using the loop is recommended for the majority of usages because the algorithm provides O(n) time complexity and O(1) space complexity, whereas the algorithm's recursive calculation can result in very slow-performing code, especially for larger input sizes.

Q4. Why is the Fibonacci Series important in coding interviews?

The ability to generate a Fibonacci series demonstrates to interviewers that the candidate understands loops, recursion, algorithm optimization, and dynamic programming.

Q5. What is the time complexity of the Fibonacci Series program in C?

The iterative implementation has an O(n) time complexity, while the simplistic recursive implementation has an O(2ⁿ) time complexity due to the continual calculation of values that have already previously calculated values.