C- Program to generate Fibonacci Series.
Fibonacci Series :
Fibonacci.c
#include<stdio.h>
int main(void)
{
long a,b,c;
int i,n;
a=0;
b=1;
printf("Enter the number of terms : ");
scanf("%d",&n);
printf("%ld ",b);
for(i=1; i<n; i++)
{
c=a+b;
printf("%ld ",c);
a=b;
b=c;
}
printf("\n");
return 0;
}
Output:
Ternimal
Enter the number of times : 10
1 1 2 3 5 8 13 21 34 55
Enter number of times : 15
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Fibonacci Series Recursive:
FibonacciRecursive.c
#include<stdio.h>
int fibonacci(int n);
int main(void)
{
int ntimes, i;
printf("Enter number of times : ");
scanf("%d",&ntimes);
for(i=0; i<ntimes; i++)
printf("%d ",fibonacci(i));
printf("\n");
return 0;
}
int fibonacci(int n)
{
if(n==0 || n==1)
return(1);
return(fibonacci(n-1) + fibonacci(n-2));
}
Output:
Terminal
Enter the number of times : 10
1 1 2 3 5 8 13 21 34 55
Enter the number of terms : 20
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
Happy Learning 🙂