Categories
Programming

Function to generate the Fibonacci numbers.

Write a function to print the Fibonacci numbers.

The fibonacci sequence is a set of numbers formed by

F_n = F_{n-1} + F_{n-2},\!\,

with seed values

F_0 = 0 \quad\text{and}\quad F_1 = 1.

This generates the numbers using the following set of rules….

 \begin{align} 0 + 1 &= 1 \\ 1 + 1 &= 2 \\ 1 + 2 &= 3 \\ 2 + 3 &= 5 \\ 3 + 5 &= 8 \\ 5 + 8 &= 13 \\  &\;\vdots \end{align}

So the fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,…

One reply on “Function to generate the Fibonacci numbers.”

// Generate the Fibonacci series
#include
#include
#define NUMTOCALC 10

int main(){
  int a = 0;
  int b = 1;
  int tmp;

  int i;
  for( i=0; i<=(NUMTOCALC-1); i++ ){
    printf("%d", b);
    tmp = b;
    b += a;
    a = tmp;

    if( i != NUMTOCALC ){
      printf(", ");
    }else{
      printf("\n");
    }
  }

  return(0);
}

Leave a Reply