The Fibonacci series in C is a number sequence in which each number is calculated by adding the previous two numbers. The series generally starts with 0 and 1, followed by 1, 2, 3, 5, 8, 13, and so on.
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.
The Fibonacci series is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers.
The first two numbers are:
- First number = 0
- Second number = 1
The sequence is:
Mathematical formula:
Where:
- F(n) represents the current Fibonacci number.
- F(n-1) represents the previous number.
- F(n-2) represents the number before the previous number.
Algorithm to Generate Fibonacci Series in C
- Start.
- Declare variables
n,first,second,next, andi. - Initialize
first = 0andsecond = 1. - Read the number of terms
n. - Repeat the following steps while
iis less thann:- Print
first. - Calculate
next = first + second. - Assign
secondtofirst. - Assign
nexttosecond.
- Print
- Stop.
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
- Initialize
first = 0andsecond = 1. - Read the number of terms from the user.
- Start a
forloop that executesntimes. - Print the value stored in
first. - Calculate the next number using
next = first + second. - Update
first = second. - Update
second = next. - Repeat until all requested terms are printed.
Dry Run of Fibonacci Series in C
Suppose the user enters 7.
| Iteration | First | Second | Next | Printed |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 0 |
| 2 | 1 | 1 | 2 | 1 |
| 3 | 1 | 2 | 3 | 1 |
| 4 | 2 | 3 | 5 | 2 |
| 5 | 3 | 5 | 8 | 3 |
| 6 | 5 | 8 | 13 | 5 |
| 7 | 8 | 13 | 21 | 8 |
Final output: 0 1 1 2 3 5 8
C Program to Print Fibonacci Series Using Recursion
Recursion is another method of generating the Fibonacci series. In this method, a function calls itself to calculate the Fibonacci number.
#include <stdio.h>
long long fibonacci(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
if (n < 0 || n > 93) {
printf("Please enter a number between 0 and 93.");
return 0;
}
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%lld ", fibonacci(i));
}
return 0;
}
Output:
Enter the number of terms: 7
Fibonacci Series: 0 1 1 2 3 5 8
How does recursion work?
- The function returns 0 when
n == 0. - It returns 1 when
n == 1. - For other values, it adds the results of
fibonacci(n-1)andfibonacci(n-2). - The function continues calling itself until it reaches the base conditions.
Time and Space Complexity
Complexity depends on the method used to generate the Fibonacci series.
| Method | Time Complexity | Auxiliary Space Complexity |
|---|---|---|
| Iterative (For Loop) | O(n) | O(1) |
| Recursive | O(2ⁿ) | O(n) |