# Best 5 Programs on Fibonacci Series in C

## Introduction

In this post, I am going to write programs on Fibonacci series in c using for loop, while loop, function and recursion. There is also program to find the sum of Fibonacci series in c language. These total 5 programs are very important.

### Fibonacci series meaning

In this series, first two numbers are 0,1 or 1,1. We are considering 0, 1. By adding these two numbers you will get the third number i.e. 1. So your series become 0, 1, 1. Now, again add the last two numbers from this series and you will get the fourth number i.e. 2 and so on.

So basically, we have to add last two numbers to get the next number in this series. Let’s see the expected output for fibonacci series in c.

### Expected Output

``````Enter value for n
10
0 1 1 2 3 5 8 13 21 34

Enter value for n
5
0 1 1 2 3``````

As you can see the above expected output. We are asking user to enter the value for n. Here, n is nothing but the total numbers in the fibonacci series.

### Flowchart for Fibonacci Series

This flowchart will help you to understand the program of fibonacci series in c.

From the above flowchart, it is very easy to understand the logic of the program. Let’s understand the step by step execution of this flowchart.

1. START
2. We are declaring the variables i.e. n1=0, n2=1, n3, n, i
3. We need to know the value of n so that we can print n number of elements in the Fibonacci series.
4. Now, we are printing the initial values of Fibonacci series using n1 and n2 i.e. 0 and 1.
5. Suppose the value of n is 10. We have already printed two numbers and we need to print the remaining 8 elements, therefore, we are initializing the value of i=3. The variable i is a loop variable.
6. Here, we have to print total n elements in the Fibonacci series. Therefore, we are repeating the following steps 7, 8 and 9 until the value of i is less than or equal to the value of n.
7. Set n3 = n1 + n2
8. Print n3
9. Set n1 = n2, n2 = n3, i = i + 1
10. STOP

Now, see the following programs.

### Fibonacci Series in C using for loop

This c program will print the fibonacci series using the for loop.

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1=0,n2=1,n3,n,i;
printf("Enter value for n");
scanf("%d",&n);
printf("%d %d",n1,n2);
for(i=3;i<=n;i++)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
``````

As you can see the above program, we have used the for loop. Now, we can write the same program using while loop.

### Fibonacci Series Program in C using while loop

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1=0,n2=1,n3,n,i;
printf("Enter value for n");
scanf("%d",&n);
printf("%d %d",n1,n2);
i=3;
while(i<=n)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
i++;
}
return 0;
}``````

I hope you have understood these two programs. Now, we will do the same program using function recursion.

### Fibonacci Series in C using Function

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
void fibonacci(int,int,int);
int n1=0,n2=1,n;
printf("Enter value for n");
scanf("%d",&n);
printf("%d %d",n1,n2);
fibonacci(n,n1,n2);
return 0;
}
void fibonacci(int num,int n1,int n2)
{
int i=3,n3;
while(i<=num)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
i++;
}
}``````

In this program, we have used the function fibonacci() and have passed three values i.e. n, n1 and n2. In the called function, we have find fibonacci series.

Now, in the next section, we are using the concept of recursion to find the fibonacci series.

### Fibonacci Series Program in C using Recursion

Recursion is nothing but a process where function calls itself. In this program, we are using the concept to find the fibonacci series.

``````#include <stdio.h>
#include <stdlib.h>
int n1=0,n2=1;
int main()
{
void fibonacci(int);
int n1=0,n2=1,n;
printf("Enter value for n");
scanf("%d",&n);
printf("%d %d",n1,n2);
fibonacci(n-2);
return 0;
}
void fibonacci(int num)
{

if(num>0)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
fibonacci(num-1);
}
}``````

In the above program, I have declared global variables i.e. n1, n2 and n3. Because I am using them in the main function as well as in the user-defined function. In the called function, we have not used for loop or while loop. We are just calling the same function again and again from the called function. But every time, we are decrementing the value of the argument by 1. So, when the value of this argument becomes zero, then this recursion will be stopped.

Also Read: C Program to Remove Zeros from a number

I hope, you are enjoying this post. We have written 4 c programs for printing fibonacci series. Now, I am going to print the addition of these fibonacci series.

See the following program.

### Sum of Fibonacci Series in C

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1=0,n2=1,n3,n,i,sum=1;
printf("Enter value for n\n");
scanf("%d",&n);
printf("%d %d",n1,n2);
for(i=3;i<=n;i++)
{
n3=n1+n2;
printf(" %d",n3);
sum=sum+n3;
n1=n2;
n2=n3;
}
printf("\nSum of the above Fibonacci Series is %d",sum);
return 0;
}``````

Here, we are using the first program from this post. But, I have added two extra lines of code here. The statement sum=sum+n3 is actually adding the value of n3 in the variable sum. When our loop is over, then we are displaying the value of this sum variable. Just see the following output.

### Output

``````Enter value for n
10
0 1 1 2 3 5 8 13 21 34
Sum of the above Fibonacci Series is 88``````

In this post, I have written 5 c programs on Fibonacci series. I hope you have understood this program. If you still have any problem or doubt, then please contact me. I will be happy to help you.

Thank You.