Sunday, July 13, 2008

print first N FIBONACCI numbers

/*Write a C program to generate and print first N FIBONACCI numbers*/

#include stdio.h

void main()
{
int fib1=0, fib2=1, fib3, N, count=0;

printf("Enter the value of N\n");
scanf("%d", &N);

printf("First %d FIBONACCI numbers are ...\n", N);
printf("%d\n",fib1);
printf("%d\n",fib2);
count = 2; /* fib1 and fib2 are already used */

while( count < N)
{
fib3 = fib1 + fib2;
count ++;
printf("%d\n",fib3);
fib1 = fib2;
fib2 = fib3;
}
} /* End of main() */

/*--------------------------
Enter the value of N
10
First 5 FIBONACCI numbers are ...
0
1
1
2
3
5
8
13
21
34
-------------------------------*/

This program is to print the fibanocci series. 0 1 1 2 3 5 8 13

The points to note in this program are two variables are preinstianted as the logic of it says that the sum of previous two numbers is the present number. Here there is a good point to note. The COUNT value is initially assigned to 0. but later changed to 2 as after execution, we only need 8 values excluding the initial value instanciated

No comments: