Today we will learn the Fibonacci Series Program in C and also Fibonacci Series in C Program using different loops and user-defined functions. But, before starting you must have knowledge of Fibonacci Series. So, we will learn it first.

What is Fibonacci Series?

The Fibonacci Series is a never-ending Sequence where the next upcoming term is the addition of the previous two terms. The Fibonacci Series starts with 0,1…

Fibonacci Series: 0,1,1,2,3,5,8,13,21 and so on.

In the above example, you will observe that the series is starting with 0,1 and the third number is 1 because of the addition of the first two numbers.

Fibonacci Series Algorithm:

1 Step: Take an integer variable X, Y and Z

2 Step: Set X=0 and Y=0.

3 Step: Display X and Y.

4 Step: Z=X+Y.

5 Step: Display Z.

6 Step: Start X=Y, Y=Z.

7 Step: Repeat from 4-6, for n times.

There are different methods to print Fibonacci Series we will see that methods one by one.

1. Fibonacci Sequence up to Certain Number

In this program, we will ask the user to enter the number up to which the user wants to print the series.

C

Output:

fibonacci series program in c

In the above program, we have declared four integer variables Number, i, First_Value, Second_Value.

Explanation:

1st Iteration: the while condition(0<5) returns TRUE so the while statement will start executing. We have used the if-else statement and condition if(0<=1) returns TRUE. So, Next=0 and then if statement block will exit. At last, i will increment.

2nd Iteration: the while condition(1<5) returns TRUE so the while statement will start executing. The condition if(1<=1) returns TRUE. So, Next=1 and then if statement block will exit. At last, i will increment to 1.

3rd Iteration: the while condition(2<5) returns TRUE and if(2<=1) returns FALSE so else statement will start executing. Next=First_Value+Second_Value=0+1=1 and the First_Value=Second_Value=1. So, Second_Value=Next=1 and i is incremented by 1.

4th Iteration: the while condition(3<5) returns TRUE and if(3<=1) returns FALSE so else statement will start executing. Next=First_Value+Second_Value=1+1=2 and the First_Value=Second_Value=1. So, Second_Value=Next=2 and i is incremented to 4.

5th Iteration: the while condition(4<5) returns TRUE and if(4<=1) returns FALSE so else statement will start executing. Next=First_Value+Second_Value=1+2=3 and the First_Value=Second_Value=2. So, Second_Value=Next=3 and i is incremented to 5.

6th Iteration: the while condition(5<5) returns FALSE and the program is exited. And the final output is 0 1 1 2 3.

2. Fibonacci Series up to n Times

This program will print Fibonacci terms up to n times.

C

Output:

fibonacci series in c program

3. Fibonacci Series in C without Recursion

C

Output:

without using recursion

4. Fibonacci Series program Using Recursion

Recursion means calling the function again and again until the condition becomes false.

C

Output:

fibinacci series using recursion

Time Complexity: T(n)=T(n-1)+T(n-2) i.e exponential.

In the above program, we can observe that the implementation does lots of repetition so this is very bad implementation for nth Fibonacci Series.

5. Using for Loop

In this program, we will use for loop instead of while loop doesn’t be afraid the logic is the same.

C

Output:

fibonacci series in c program

6. Using dynamic Programming

we can store the Fibonacci number to avoid repetition.

C

Output:

dynamic programming

7. Fibonacci Series in C program Using Functions

In this program, we will use a user-defined function Fibonacci_Series to print a Fibonacci Series Up to n number.

C

Output:

fibonacci series using functions

Also Read: