Friday, September 9, 2016

Fibonacci Series

It is a series of numbers such that the first number is 0, second is 1 and from thereon every number is a sum of previous two numbers.
0 1 1 2 3 5.....

The concept can be extended for negative numbers also  -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8,
For the sake of simplicity we re considering non negative sequence only.



#include<stdio.h>

int main()
{
int n;

printf("enter the number of fibonacci numbers you want to enter \n \n");
scanf_s("%d", &n);

if (n <= 0)
printf("not valid or negative sequence \n");

else if (n == 1)
printf("0 \n");
else if(n==2)
printf("0 1\n");

else
{
int a2 = 0;
   int a1 = 1;
int newnum;
printf("0 \n1\n");
for (int i = 2; i < n; i++)
{
newnum = a2 + a1;
printf("%d \n", newnum);
a2 = a1;
a1 = newnum;
}


}

return 0;

}


O/P-





No comments:

Post a Comment