Write a program of Fibonacci series in C language

Fibonacci series is the sum of previous two numbers in the sequence of numbers.

Example:

0 1 1 2 3 5 8 …….

Here first and second terms are 0 & 1.Here third term is 1 which is the sum of (0+1).

1.By using for loop in C

#include<stdio.h>
Void main()
{
Int i,a=0,b=1,sum,swap,n=5;
printf(“Fibonacci series is:\n%d \n%d”,a,b);
for( i=2;i<n;i++)
{
sum=a+b;
printf(“%d\n”,sum);
swap=a;
a=b;
b=swap;
}
}

Output:

Fibonacci series is:
0
1
1
2
3

2.Fibonacci series using the recursion function in C.

#include<stdio.h>
Void Fibonacci(int n);
{
Static Int a=0,b=1,n,swap,sum;
if(n>0)
{
sum=a+b;
a=b;
b=swap;
printf(“%d”,sum);
Fibonacci(n-1);
}
Int main()
{
Int n;
printf(“Enter the number\n”);
scanf(“%d”,&n);
printf(“Fibonacci series is given below\n”);
printf(“%d%d”,0,1);
Fibonacci(n-2);
}

Output:

Enter the number
5
Fibonacci series is given below
0 1 1 2 3

3.Fibonacci series using the Array in C

#include<stdio.h>
Void main()
{
Int i,array[10],n;
printf(“Enter the fibonacci terms\n”);
scanf(“%d”,&n);
array[0]=0;
array[1]=1;
for(int i=2;i<n;i++)
{
array[i]=array[i-1]+array[i-2];
}
//The below code is used for the display the output
printf(“Fibonacci series  is given \n”);
for(int i=0;i<n;i++)
{
printf(array[i]);
}
}

Output:

Enter the fibonacci  terms
5
Fibonacci series  is given
0 1 1 2 3

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *