Study Guides/Computer Science/Fibonacci Series in C
Study Guide ยท Computer Science

Fibonacci Series in C (Code and Logic)

The Fibonacci Series is one of the most famous sequences in mathematics and computer science. In this series, every number is the sum of the two numbers before it. Starting from 0 and 1:

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

Question (Click to Flip)

What is the 10th Fibonacci number?

Answer

Starting from the 1st term as 0: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. The 10th term is 34.

Card 1 of 1 free previews

Key Facts

The Fibonacci sequence appears throughout nature โ€” in the spiral arrangement of a sunflower's seeds, the pattern of a pine cone, the branching of trees, and the spiral shell of the nautilus. This is sometimes called the 'fingerprint of nature'.

Method 1: Iterative Fibonacci Program in C

#include <stdio.h>
int main() {
    int n, a = 0, b = 1, c;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    printf("Fibonacci Series: ");
    for (int i = 1; i <= n; i++) {
        printf("%d ", a);
        c = a + b; // next term = sum of previous two
        a = b;    // update a
        b = c;    // update b
    }
    return 0;
}

Output for n=8: 0 1 1 2 3 5 8 13

Method 2: Recursive Fibonacci Program in C

#include <stdio.h>
int fibonacci(int n) {
    if (n == 0) return 0;
    if (n == 1) return 1;
    return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
    int n;
    printf("Enter terms: ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        printf("%d ", fibonacci(i));
    }
    return 0;
}

Note: The iterative method is much more efficient. The recursive method has O(2โฟ) time complexity (exponential โ€” very slow for large n), while the iterative method is O(n) (linear โ€” much faster).

Questions and Answers

What is the 10th Fibonacci number?+

Starting from the 1st term as 0: 0, 1, 1, 2, 3, 5, 8, 13, 21, **34**. The 10th term is **34**.

More in Computer Science

Study Smarter with Shinyu.ai

Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ€” free, no signup required.