C Program for Fibonacci Series


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 nth 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 nth term of the Fibonacci series can be represented as:

               tn = tn-1 + tn-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 ?

C Program for 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.

C Program for Fibonacci Series - Fibonacci Numbers
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:

C Program for Fibonacci Series - Golden Ratio
Golden Ratio of Fibonacci Sequence

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

C Program for Fibonacci Series - Fibonacci Numbers in Pascal's Triangle
Fibonacci Numbers in 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.

C Program for Fibonacci Series
5. A Pattern: In the Fibonacci series, every nth number is a multiple of xnth number.

  • x3 = 2. Every 3rd number is a multiple of 2 (2, 8, 34, 144, 610, …)
  • x4 = 3. Every 4th number is a multiple of 3 (3, 21, 144, …)
  • x5 = 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.

C Program for Fibonacci Series - Terms Below Zero

Fibonacci Series in C Without Using Function:

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 - Output

Fibonacci Series in C Using Recursive Function:

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.



Please enter your comment!
Please enter your name here