Fibonacci Series Using an Array




Fibonacci Number:
It is an interesting sequence in which each number is eqal to the sum of previous two numbers. As follows:
0 1 1 2 3 5 8 13 21 34 55 89 ……………………………………
This is a program which prints out a table of Fibonacci numbers. This program stores the series in an array, and after calculating it, prints the numbers out as a table.


#include<stdio.h>
#include<conio.h>
void main()
{


int fib[100];
int i,n;
clrscr();
printf("How many Fibonacci numbers you want:");
scanf("%d",&n);
fib[0] = 0;
fib[1] = 1;
printf("\n\nSerial Fibonacci number\n\n");
for(i = 2; i < n; i++)
fib[i] = fib[i-1] + fib[i-2];

for (i = 0; i < n; i++)
printf("%3d %6d\n", i, fib[i]);
getch();
}



int fib[100];
This defines an array called fib of 100 integers. We in C, the array index is enclosed by square brackets (it avoids misunderstanding with a function call).One can choose how many Fibonacci number he want less then 100.for this I use int n.Also, the first element of the array(fib[0]) has index zero, and for this n element array, the last element is index n-1.

1 comment: