Problem Statement
In this program we will code a basic program to print Fibonacci Series in C Programming Language.
Code
#include<stdio.h>
void fibonacci(int n)
{
int fir,sec,thi,i;
fir=0;
sec=1;
printf("%d %d ",fir,sec);
thi=fir+sec;
for(i=1;i<=n-2;i++)
{
printf("%d ",thi);
fir=sec;
sec=thi;
thi=fir+sec;
}
}
int main()
{
int n;
printf("How many elements ? : ");
scanf("%d",&n);
fibonacci(n);
return 0;
}
Output
How many elements ? – 6
0 1 1 2 3 5
Time Complexity
O(n) // We also have most optimized solution for finding the fibonacci series – O(logn)
Explanation
In this problem we have to find the fibonacci series upto ‘N’ elements.The Fibonacci series represents the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones. Ex : 0,1,1,2,3,5,8,13,21,34,55,89,144….