# Fibonacci series program in C using iteration. Ans:

In this tutorial we are going to learn how to print Fibonacci series in C program using iterative method.

In this program count of output elements of the series is depends upon the input given by users. Program will print n number of elements in a series, where n is a number given by the user as a input.

Before moving directly on the writing Fibonacci series in c program, first you should know

A Fibonacci series is a series in which next number is a sum of previous two numbers.

For example : 0, 1, 1, 2, 3, 5, 8 ……

In Fibonacci Series, first number starts with 0 and second is with 1 and then its grow like,

0

1

0 + 1 = 1

1 + 1 = 2

1 + 2 = 3

2 + 3 = 5 and

so on…

## How our program will behave?

Suppose if someone gives an input 5 then our program should print first 5 numbers of the series.

Like if someone given 5 as a input then our Fibonacci series program which is written in C should print output as,

0, 1, 1, 2, 3

## C program to print Fibonacci series using Iterative methods

``````#include <stdio.h>
#include <conio.h>
int main()
{
int n, first = 0, second = 1, result, i;
printf("Please give an input upto you want to print series : \n");
scanf("%d", &n);
printf("Fibonacci Series is:\n");
for (i = 0; i < n; i++)
{
if (i <= 1)
result = i;
else
{
result = first + second;
first = second;
second = result;
}
printf("%d\n", result);
}
return 0;
} ``````

Output: ## Explanation of Fibonacci series in C program using Iterative method

• In the above program I have taken 5 variables of integer type.
• Variables are n, i, first, second and result.
• Variable first and second has assigned value 0 and 1 respectively. Because we have assumed that our first and second number of this is series is fixed. And further number we will calculate on the basis of this two first elements.
• Now we have a “for loop”, where the logic is written. This loop will be execute n number of times where n is  number given as input.
• result is a variable which will hold the final result after calculation and print each time. If the input is given 0 then program will print 0 and if input is 1 then it will print 0, 1.
• But if it greater than 1 then calculation required and as per calculation it will print the output.
• For 3rd element of the series, result will hold the sum of first and second number.
• After calculation we will assign value of second variable in first variable. And latest calculated value in second variable.
• This new value will be use to calculate next element of the Fibonacci series.
• This logic will be apply for the calculation of all next elements of the Fibonacci series.

I hope above program is now clear to you.