Generate a Fibonacci series in C

This is an example C program for generating a Fibonacci series.

#include <stdio.h>

int main ()
{
    int i;
    int max;
    max = 20;
    int fib[max];
    fib[0] = 0;
    fib[1] = 1;

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

    return 0;
}

(download)

The output of the example looks like this:

Fibonacci sequence from 1 to 20:
1:0 2:1 3:1 4:2 5:3 6:5 7:8 8:13 9:21 10:34 11:55 12:89 13:144 14:233 15:377 16:610 17:987 18:1597 19:2584 20:4181 


Copyright © Ben Bullock 2009-2017. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com). / Privacy / Disclaimer