Back

C Program For Fibonacci Series

11 Dec 2024
6 min read

The Fibonacci series is a sequence of numbers where each number is the sum of the two numbers before it. It starts with 0 and 1. So, it goes like this: 0, 1, 1, 2, 3, 5, 8, 13, and so on. The C program for Fibonacci series is a classic problem in coding. Writing a Fibonacci series teaches you how to work with loops, recursion, and basic algorithms. So, let’s look at how to write this code and apply it successfully.

What is Fibonacci Series?

The Fibonacci series is a sequence where each number is the sum of the two numbers that come before it. The problems usually start with 0 and 1. In mathematics, it’s expressed using the recurrence relation:

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

With the starting conditions:

F(0) = 0

F(1) = 1

The simple formula of this series can be used to build the entire sequence. Fibonacci series forms the foundation of many mathematical concepts and real world applications so learning to write a program for it is important.

Fibonacci Series Program in C Using Recursion

Now let’s look at the Fibonacci series in c recursion. This program prints the Fibonacci series using recursion. The printFib() function handles edge cases for n less than 3 and prints the first two terms for larger values. It then calls the recursive fib() function, which calculates each term as the sum of the previous two and prints them recursively.

// C Program to print the Fibonacci series using recursion
#include <stdio.h>

// Recursive function to print the fibonacci series
void fib(int n, int prev1, int prev2) {
      // Base Case: when n gets less than 3
    if (n < 3) {
        return;
    }
      
    int curr = prev1 + prev2;
    prev2 = prev1;
    prev1 = curr;
    printf("%d ", curr);
    return fib(n - 1, prev1, prev2);
}

// Function that handles the first two terms and calls the
// recursive function
void printFib(int n) {
  
    // When the number of terms is less than 1
    if (n < 1) {
        printf("Invalid number of terms\n");
    }
    // When the number of terms is 1
    else if (n == 1) {
        printf("%d ", 0);
    }
    // When the number of terms is 2
    else if (n == 2) {
        printf("%d %d", 0, 1);
    }
  // When number of terms greater than 2
    else {
        printf("%d %d ", 0, 1);
        fib(n, 0, 1);
    }
    return;
}

int main() {
    int n = 9;
  
      // Printing first 9 fibonacci series terms
    printFib(n);
  
    return 0;
}

Output:

0 1 1 1 2 3 5 8 13

Fibonacci Series in C Using For Loop

In this program, we generate the Fibonacci series using a for loop. When you run it, the user is prompted to enter how many Fibonacci numbers to display. Say you want it to show the first 15 Fibonacci numbers, starting with 0 and 1. It prints the current number (num1) and calculates the next number by adding the previous two. The loop repeats until all terms are printed.

#include <stdio.h>

int main() {
    int n, num1 = 0, num2 = 1, nextNum;

    printf("Enter the number of Fibonacci numbers to generate: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (int i = 1; i <= n; ++i) {
        printf("%d, ", num1);
        nextNum = num1 + num2;
        num1 = num2;
        num2 = nextNum;
    }

    return 0;
}

Output:

Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

Fibonacci series in C Using While Loop

This C Fibonacci program generates the series using a while loop. The user just has to enter the number of terms to display in the Fibonacci series. Starting with 0 and 1, it prints the current number (num1). It calculates the next term by adding the previous two and repeats the process until the specified number of terms is printed.

#include <stdio.h>

int main() {
    int n, num1 = 0, num2 = 1, nextNum, count = 0;

    printf("Enter the number of Fibonacci numbers to generate: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    while (count < n) {
        printf("%d, ", num1);
        nextNum = num1 + num2;
        num1 = num2;
        num2 = nextNum;
        count++;
    }

    return 0;
}

Output:

Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

Fibonacci Series Program in C Using Function

In this Fibonacci C code, we can generate the series using a separate function called Fibonacci (). The function takes the number of terms (n) as input and uses a loop to calculate and print the series. The user has to input the value of n in the main() function, which then calls the Fibonacci () function to display the sequence.

#include <stdio.h>

// Function to print Fibonacci series up to n terms
void fibonacci(int n) {
    int num1 = 0, num2 = 1, nextNum;
  
    for (int i = 1; i <= n; i++) {
        printf("%d ", num1);
        nextNum = num1 + num2;
        num1 = num2;
        num2 = nextNum;
    }
}

int main() {
    int n;
  
    // Get the number of terms from the user
    printf("Enter the number of Fibonacci numbers to generate: ");
    scanf("%d", &n);
  
    // Call the Fibonacci function
    printf("Fibonacci Series: ");
    fibonacci(n);
  
    return 0;
}

Output:

Enter the number of Fibonacci numbers to generate: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Conclusion

The Fibonacci series is a fundamental program that every beginner or a student in coding should learn. It introduces key concepts like loops, recursion, and functions, which form the basics for solving more complex problems. Practising the C program for Fibonacci series in different ways helps strengthen logical thinking and coding skills. You will also learn how the same task can be approached with multiple solutions. It is a critical skill in software development. Mastering such basics lays a strong foundation for becoming a confident and capable programmer. To learn more and get started on your professional journey, join a certification program like CCBO 4.0 Academy to build your coding skills from the ground up.

Frequently Asked Questions

1. What is the Fibonacci series in C programming?

The Fibonacci series is a sequence where each number is the sum of the previous two. In c Fibonacci program, it can be calculated using loops, recursion, or functions.

2. Why is the Fibonacci series important for beginners?

The C programming fibonacci series is a basic program that teaches logic, algorithms, and concepts like recursion and iteration. Practicing it strengthens problem solving skills and helps understand different ways to approach the coding problem.

3. How is recursion used in the Fibonacci series?

Recursion solves the Fibonacci series by calling the same function repeatedly. Each call calculates F(n) as the sum of F(n-1) and F(n-2) until given output.

4. Which method is better for Fibonacci loops or recursion?

Loops are faster and more memory efficient for the Fibonacci series. But recursion sticks to the mathematical definition. But it is a little slower.

5. Can the Fibonacci series program be extended?

The series can be extended to calculate Fibonacci terms for large values using arrays or dynamic programming.

Read More Articles

Chat with us
Talk to career expert