Printing Fibonacci Series in the standard format is one of the very famous programs in C programming language. This can be done either by using iterative loops or by using recursive functions. In this post, source codes in C program for Fibonacci series has been presented for both these methods along with a sample output common to both. And, in order to make the source code user-friendly or easier for you to understand, I have included multiple comments in the program source code.

Generally, Fibonacci series can be defined as a sequence of numbers in which the first two numbers are 1 and 1, or 0 and 1, depending on the selected beginning point of the sequence, and each subsequent number is the sum of the previous two.

So, in this series, the *n*th term is the sum of *(n-1)*^{th} term and *(n-2)*^{th} term. Before taking you through the source code program for Fibonacci series in C, first let me explain few things about this series, it’s mathematical derivation and properties.

Mathematically, the *n*th term of the Fibonacci series can be represented as:

t_{n} = t_{n-1} + t_{n-2}

The Fibonacci numbers upto certain term can be represented as: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144….. or 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144….. As the number of term increases, the complexity in calculation and chance of occurrence of error also increases.

## How to Generate Fibonacci Series ?

The series starts with either 0 or 1 and the sum of every subsequent term is the sum of previous two terms as follows:

First Term = 0

Second term = 1

Third Term = First + Second = 0+1 =1

Fourth term = Second + Third =1+1 = 2

Fifth Term = Third + Fourth = 2+1 = 3

Sixth Term= Fourth + Fifth = 3+2 = 5

Seventh Term = Fifth + Sixth = 3+5 = 8

Eighth Term = Sixth + Seventh = 5+8 = 13 … and so on to infinity!

## Properties of Fibonacci Series:

**1. Fibonacci Numbers**: The sum of first and second term is equal to the third term, and so on to infinity. This main property has been utilized in writing the source code in C program for Fibonacci series.

**2. Golden Ratio**: The ratio of any two consecutive terms in the series approximately equals to 1.618, and its inverse equals to 0.618. This ratio known as the Golden ratio has been demonstrated in the table given below:

**3. Fibonacci Numbers in Pascal’s Triangle**: The Fibonacci numbers are found in the shallow diagonal of the Pascal’s Triangle.

**4. Make a Spiral**: Go on making squares with dimensions equal to the widths of terms of the Fibonacci sequence, and you will get a spiral as shown below. Here 5 and 8 make 13, 8 and 13 make 21, and so on.

**5. A Pattern**: In the Fibonacci series, every *n*th number is a multiple of * xn*th number.

- x
_{3}= 2. Every 3rd number is a multiple of 2 (2, 8, 34, 144, 610, …) - x
_{4}= 3. Every 4th number is a multiple of 3 (3, 21, 144, …) - x
_{5}= 5. Every 5th number is a multiple of 5 (5, 55, 610, …)

**6. Terms Below Zero**: You probably didn’t know this – there are term below 0 (zero) in the Fibonacci series. Below zero, the sequence has the same numbers as the series above zero, except that they follow a + – + – … sign pattern.

## Fibonacci Series in C Without Using Function:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /*C Program for Fibonacci Series without using Function*/ #include <stdio.h> int main() { int i, n, a=0, b=1, show=0; printf("\nEnter number of terms required in Fibonacci Series: "); scanf("%d",&n); printf("\nThe Fibonacci Series is:\n\n\n %d+%d+", a, b); /* Showing the first two term of the Fibonacci Series */ i=2; /* i=2, since the first two terms of the series have already been shown*/ while (i<n) { show=a+b; a=b; b=show; ++i; printf("%d+",show); } return 0; } |

The above source code in C program for Fibonacci series is very simple to understand, and is very short – around 20 lines. In this code, instead of using function, I have used loops to generate the Fibonacci series. This program has been developed and compiled in Code::Blocks IDE using GCC compiler. To run the program, copy the aforementioned code in Code::Blocks.

Now here’s how the code works. You need to provide the number of terms to be generated in the series, and this is stored in the form of interger type of varible *n*. After getting the value of n, the program displays the first two terms of the series and then starts calculation of other terms.

As already stated before, the basic working principle of this C program for Fibonacci Series is that “each term is the sum of previous two terms”. Therefore, two sequent terms are added to generate a new term. The process continues till the last term of the series is obtained.

## Fibonacci Series in C Using Recursive Function:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /*C Program for Fibonacci Series using Recursive Function*/ #include<stdio.h> #include<conio.h> int fun(int x)// Function the defination of function fun() { int f; if (x==1) return(0); else if (x==2) return(1); else f= fun(x-1)+fun(x-2); return(f); } void main() { int fun(int ) ; int x,y,n; printf("Enter number of terms required in Fibonacci Series:"); scanf("%d",&n); printf("\nThe Fibonacci Series is:\n\n\n"); for(x=1; x<=n; x++) { y=fun(x);// Calling of Function fun() with Argument printf("%5d",y); } getch(); } |

The output of the Fibonacci series in C given above is similar to the sample output included in this post. The only difference between these two programs is that this source code utilizes recursive function to print Fibonacci series in standard format, but the other program code utilizes loops for control of numbers in the series.

As with the previous source code, in this program for Fibonacci series using recursive function, you need to input the number of terms to be printed. This is stored as variable n, which should be defined as an integer data type. Other two variables used in the source code are x & y, which have been used in function calling and sending & returning arguments.

Also see,

Fibonacci Series Algorithm/Flowchart

Pascal’s Triangle C Program

Floyd’s Triangle C Program

The source codes aforementioned in this post are totally error-free, and have been coded perfectly to eradicate bugs. If you have any other alternative C source codes related to Fibonacci series, you can share them with us.

Also, your queries, feedbacks and suggestions relevant to this program for Fibonacci Series in C language, can be discussed in, and brought directly to us from the comments section below.

Thanks for this

it is best add more example for C++ program takers.

sir Fibonacci serial between two number

6-20

8,13 like this

5-30

5,8,13,21 like this

Good question. I’d be glad to see someone coming up with a source code to print the Fibonacci series in that way 🙂 Give it a try first Vidhyadhar; send me the code you come up with, and I will help you if needed.

good i like it