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
What is Fibonacci Series?
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 :");
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 1:
Please give an input upto you want to print series :12
Fibonacci Series is:
0
1
1
2
3
5
8
13
21
34
55
89
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.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…