C Program to Generate N Fibonacci Terms Using Array

The Fibonacci sequence starts with 0 and 1; each subsequent term is the sum of the two that precede it: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … This program generates the first n terms by storing them in an array, then printing the full sequence. Using an array — rather than just two variables — lets you access any previously generated term directly by index. The original post had no code; this is a complete C89-compliant implementation.

How It Works

  1. Seed the array: fib[0] = 0, fib[1] = 1.
  2. For each index i from 2 to n−1: fib[i] = fib[i-1] + fib[i-2].
  3. Print all n elements.
i fib[i-2] fib[i-1] fib[i]
2 0 1 1
3 1 1 2
4 1 2 3
5 2 3 5
6 3 5 8
7 5 8 13

C Program: Fibonacci Sequence Using Array

/* Generate n Fibonacci terms using an array
 * Compile: gcc -ansi -Wall -Wextra fibarray.c -o fibarray */
#include <stdio.h>
#define MAX 50

int main(void)
{
    int fib[MAX];
    int n, i;

    printf("How many Fibonacci terms? (1-%d): ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        printf("Enter a number between 1 and %d.\n", MAX);
        return 1;
    }

    fib[0] = 0;
    if (n > 1)
        fib[1] = 1;

    for (i = 2; i < n; i++)
        fib[i] = fib[i - 1] + fib[i - 2];

    printf("Fibonacci sequence (%d terms):\n", n);
    for (i = 0; i < n; i++) {
        printf("%d", fib[i]);
        if (i < n - 1) printf(", ");
    }
    printf("\n");

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra fibarray.c -o fibarray
./fibarray

Sample Output

How many Fibonacci terms? (1-50): 10
Fibonacci sequence (10 terms):
0, 1, 1, 2, 3, 5, 8, 13, 21, 34

How many Fibonacci terms? (1-50): 1
Fibonacci sequence (1 terms):
0

How many Fibonacci terms? (1-50): 2
Fibonacci sequence (2 terms):
0, 1

Code Explanation

  • Seed valuesfib[0] = 0 and fib[1] = 1 are set before the loop. The loop starts at i = 2 because it reads fib[i-1] and fib[i-2]; if it started at 0 or 1, those accesses would read uninitialized memory. The guard if (n > 1) fib[1] = 1; handles the edge case where the user asks for exactly 1 term — in that case, fib[1] is never read, so it doesn’t need to be set.
  • Array vs two-variable approach — a two-variable loop (prev, curr) uses less memory and is sufficient when you only need to print the sequence. The array approach is more general: after the loop, any term is accessible by index without recomputing it. This matters in algorithms that need random access to earlier Fibonacci numbers, such as the Zeckendorf representation.
  • #define MAX 50 — the upper bound is defined as a named constant. Changing it to 60 requires editing one line. With int (32-bit), the 46th Fibonacci number (1,836,311,903) is the last that fits; the 47th overflows. To generate more terms, use long or unsigned long long.
  • Comma formatting in the print loop — printing a comma only when i < n - 1 avoids a trailing comma after the last element. This is the standard idiom for delimiter-separated output in C.

Integer Overflow Note

With int (typically 32 bits, max 2,147,483,647), the sequence overflows at fib[47]. For a reliable 50-term sequence use long long and %lld:

long long fib[MAX];
/* ... */
printf("%lld", fib[i]);

What This Program Teaches

  • Bottom-up dynamic programming — filling an array from base cases upward, using previously stored results to compute each new one, is the iterative DP pattern. It avoids the exponential recomputation of naive recursion. The same pattern applies to longest-common-subsequence, coin change, and edit distance.
  • Edge case handling — n=1 and n=2 are boundary cases that the loop body never handles (it starts at i=2). Explicitly seeding fib[0] and conditionally seeding fib[1] is cleaner than special-casing the print loop.
  • Integer overflow awareness — fixed-width integer arrays have a natural limit. Always know the maximum value your computation can produce and pick the right type accordingly. unsigned long long extends the usable range to about 93 terms.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

2 comments on “C Program to Generate N Fibonacci Terms Using Array

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>