Here’s a C program to find the sum of Fibonacci series with output and proper explanation. The program uses for loop.

Enter the limit : 5

The fibonacci series is : 0 1 1 2 3

The sum of the fibonacci series is : 7

What is Fibonacci Series?By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. e.g.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

# include <stdio.h> # include <conio.h> void main() { int a = -1, b = 1, c = 0, i, n, sum = 0 ; clrscr() ; printf("Enter the limit : ") ; scanf("%d", &n) ; printf("\nThe fibonacci series is : \n\n") ; for(i = 1 ; i <= n ; i++) { c = a + b ; printf("%d \t", c) ; sum = sum + c ; a = b ; b = c ; } printf("\n\nThe sum of the fibonacci series is : %d", sum) ; getch() ; }

**Output of above program**Enter the limit : 5

The fibonacci series is : 0 1 1 2 3

The sum of the fibonacci series is : 7

**Explanation of above program**

This program first calculates the Fibonacci series up to a limit and then calculates the sum of numbers in that Fibonacci series.

The program has several variables -

- a, b, c - These integer variables are used for the calculation of Fibonacci series. The values of a, b and c are initialized to -1, 1 and 0 respectively.
- n - This integer is the limit determining the number of elements of Fibonacci series that should be calculated.
- sum - It is the sum of elements of Fibonacci series.
- i - This is the loop variable.

First the user enters a limit. After that for loop takes the control and
calculates the sum of Fibonacci series up to the previously entered
limit.

To understand the working of for loop in this program, let’s take an
example. Suppose the limit is 5 i.e. n = 5. So the for loop will
executes for i = 1 to 5. Let’s go through each step one by one.

Note: The process mentioned below is the standard way to calculate Fibonacci series so just remember the steps as is.

**Process for i = 1**

- First we update the value of c (initially 0) with the following
expression -
**c = a + b**. So after this c = -1 + 1 = 0 (first value of Fibonacci series). - Then we print the value of c. Notice \t is used to provide 8 spaces (1 tab) between two values see in the output.
- Then we update the value of variable sum as - sum = sum + c = 0 + 0 = 0.
- At last we update the value of a as -
**“a = b”**and then the value of b as -**“b = c”**. So after this step a = 1 and b = 0.

**Process for i = 2**

- We again update the value of c like before. So after this c = a + b = 1 + 0 = 1 (second value of Fibonacci series).
- Then we print the value of c.
- Then we update the value of variable sum as - sum = sum + c = 0 + 1 = 1.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 0 and b = 1.

**Process for i = 3**

- We again update the value of c like before. So after this c = a + b = 0 + 1 = 1 (third value of Fibonacci series).
- Then we print the value of c.
- Then we update the value of variable sum as - sum = sum + c = 1 + 1 = 2.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 1 and b = 1.

**Process for i = 4**

- We again update the value of c like before. So after this c = a + b = 1 + 1 = 2 (fourth value of Fibonacci series).
- Then we print the value of c.
- Then we update the value of variable sum as - sum = sum + c = 2 + 2 = 4.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 1 and b = 2.

**Process for i = 5**

- We again update the value of c like before. So after this c = a + b = 1 + 2 = 3 (third value of Fibonacci series).
- Then we print the value of c.
- Then we update the value of variable sum as - sum = sum + c = 4 + 3 = 7.
- At last we update the value of a as - “a = b” and then the value of b as - “b = c”. So after this step a = 2 and b = 3.

This is how we calculate the Fibonacci series and sum of Fibonacci
series.

Also look at:Program to Calculate Fibonacci Series only.

Tip:To understand the program you can also take different values of n and try the above process yourself. It will help you to understand the process much clearly and helps in improving your debugging ability.