C Program To Display Fibonacci Sequence

Fibonacci Series In C

Fibonacci in C shows up in programming classes and job interviews. It’s perfect for learning loops, recursion, and why some code runs fast while other code crawls.

C makes the performance differences really obvious. Write it wrong and you’ll wait forever for results. Write it right and it runs instantly. That’s a valuable lesson.

What’s the Fibonacci Sequence?

It’s just adding the previous two numbers:

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

Fibonacci Sequence

Start with 0 and 1. Then:

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

Keep going. The math rule is F(n) = F(n-1) + F(n-2).

Academy PRO

C Programming Course: Master C with Hands-on Projects

Join our C Programming Course and learn C syntax, operators, expressions, control flow, functions, pointers, structures, file handling, memory management, and modular programming. Build real-world skills through practical projects!

2 Projects
10 Hrs
C Programming Course with Certificate

Method 1: Simple Loop (Best Choice)

This is what you want 95% of the time. You initialize the first two numbers, then loop to calculate the rest.


#include <stdio.h>

void printFibonacci(int n) {
    long long first = 0, second = 1, next;
    
    if (n >= 1) {
        printf("Fibonacci Series: ");
        printf("%lld ", first);
    }
    if (n >= 2) {
        printf("%lld ", second);
    }
    
    for (int i = 3; i <= n; i++) {
        next = first + second;
        printf("%lld ", next);
        first = second;
        second = next;
    }
    printf("\n");
}

int main() {
    int terms;
    printf("How many Fibonacci numbers? ");
    scanf("%d", &terms);
    
    if (terms <= 0) {
        printf("Please enter a positive number.\n");
        return 1;
    }
    
    printFibonacci(terms);
    return 0;
}

Why this works:

  • Fast: calculates each number once
  • Uses almost no memory
  • Handles edge cases properly
  • Works for numbers up to the 92nd Fibonacci number

Note: We use long long instead of int because Fibonacci numbers get huge fast. Regular int overflows around the 47th number.

Method 2: Recursion (Looks Nice, Runs Terrible)

This matches the math formula but has a big problem:


#include <stdio.h>

long long fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n-1) + fibonacci(n-2);
}

int main() {
    int terms;
    printf("How many terms? ");
    scanf("%d", &terms);
    
    printf("Fibonacci Series: ");
    for (int i = 0; i < terms; i++) {
        printf("%lld ", fibonacci(i));
    }
    printf("\n");
    
    return 0;
}

The problem: Try fibonacci(40). It’ll take minutes because it recalculates everything repeatedly. fibonacci(3) gets calculated dozens of times just to find fibonacci(10).

This is O(2^n) time complexity – exponentially slow. Don’t use this for anything real.

Method 3: Smart Recursion with Memory

If you want recursion that actually works, store your results:


#include <stdio.h>

long long memo[100] = {0}; // Global array, initialized to zeros

long long fibonacci_memo(int n) {
    if (n <= 1) {
        return n;
    }
    
    // Already calculated? Return stored result
    if (memo[n] != 0) {
        return memo[n];
    }
    
    // Calculate and store
    memo[n] = fibonacci_memo(n-1) + fibonacci_memo(n-2);
    return memo[n];
}

int main() {
    int terms;
    printf("How many terms (max 92)? ");
    scanf("%d", &terms);
    
    printf("Fibonacci Series: ");
    for (int i = 0; i < terms; i++) {
        printf("%lld ", fibonacci_memo(i));
    }
    printf("\n");
    
    return 0;
}

This fixes the recursion by remembering previous results. Now it’s O(n) instead of O(2^n).

Method 4: Return Single Number

Sometimes you just want the nth Fibonacci number:


#include <stdio.h>

long long fibonacci_single(int n) {
    if (n <= 1) {
        return n;
    }
    
    long long prev = 0, curr = 1, temp;
    
    for (int i = 2; i <= n; i++) {
        temp = prev + curr;
        prev = curr;
        curr = temp;
    }
    
    return curr;
}

int main() {
    int n;
    printf("Which Fibonacci number do you want? ");
    scanf("%d", &n);
    
    if (n < 0) {
        printf("Please enter a non-negative number.\n");
        return 1;
    }
    
    printf("F(%d) = %lld\n", n, fibonacci_single(n));
    return 0;
}

Performance Comparison

I tested these on my computer with n=40:

MethodTimeMemory Usage
Simple loopInstantVery low
Basic recursion30+ secondsHigh (stack)
Memoized recursionInstantMedium

The loop version is almost always your best bet.

Common Pitfalls

Integer Overflow


// DON'T do this - will overflow quickly
int fib = fibonacci(50); 

// DO this instead
long long fib = fibonacci(50);

Array Bounds


// Dangerous - no bounds checking
long long memo[100];
return memo[n]; // What if n > 99?

// Better - check bounds first
if (n >= 100) {
    printf("Number too large for our array\n");
    return -1;
}

Forgetting Base Cases


// This will crash - infinite recursion
long long bad_fib(int n) {
    return bad_fib(n-1) + bad_fib(n-2); // No stopping condition!
}

Real-World Tips

For homework: Use the simple loop. It’s fast and shows you understand efficiency.

For interviews: Start with the loop, then explain why basic recursion is slow, then show the memoized version if they ask.

For production code: Definitely use the loop. It’s reliable and fast.

Testing your code:


// These should work:
// F(0) = 0
// F(1) = 1  
// F(10) = 55
// F(20) = 6765

Quick Questions

Why do big Fibonacci numbers turn negative?
Integer overflow. The number got too big for the data type. Use long long instead of int.

Which method should I memorize?
The simple loop version. It handles 90% of situations and runs fast.

What about really huge Fibonacci numbers?
You’d need a library for arbitrary-precision arithmetic. Standard C data types max out around F(92).

Any gotchas for beginners?
Watch your array bounds if using memoization. And remember that C doesn’t initialize arrays to zero automatically (except globals).

The loop method is fast, simple, and works reliably. Master that one first, then learn the others for understanding different programming techniques.

Also Read:

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.
Scroll to Top