The Fibonacci series is a sequence in which each term is the sum of the previous two terms. It starts with 0 and 1, and each subsequent term is generated using these two values.
- Each next term is calculated as previous term + current term.
- The series can be printed using loops or recursion.
Examples
Input: n = 5
Output: 0 1 1 2 3
Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3.Input: N = 7
Output: 0 1 1 2 3 5 8
Explanation: The first 7 terms of the Fibonacci series are 0, 1, 1, 2, 3, 5, 8.
Approaches to Print Fibonacci Series
There are two common approaches to print the Fibonacci series in C: using loops and using recursion.
Fibonacci Formula: Fn​=Fn−1​+Fn−2​
Print Fibonacci Series Using Loops
The iterative approach uses a loop and two variables to keep track of the previous two Fibonacci terms. The next term is calculated by adding these two values.
#include <stdio.h>
void printFib(int n) {
// If the number of terms is smaller than 1
if (n < 1) {
printf("Invalid Number of terms\n");
return;
}
// First two terms of the series
int prev1 = 1;
int prev2 = 0;
// for loop that prints n terms of fibonacci series
for (int i = 1; i <= n; i++) {
// Print current term and update previous terms
if (i > 2) {
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
}
else if (i == 1)
printf("%d ", prev2);
else if (i == 2)
printf("%d ", prev1);
}
}
int main() {
int n = 9;
// Printing first n fibonacci terms
printFib(n);
return 0;
}
Output
0 1 1 2 3 5 8 13 21
Explanation
- prev1 and prev2 store the previous two Fibonacci terms.
- The loop prints the current term and calculates the next term.
- After calculating the next term, the variables are updated for the next iteration.
- The process continues until n terms are printed.
Print Fibonacci Series Using Recursion
The recursive approach generates the remaining Fibonacci terms by passing the previous two terms to each recursive call. The recursion continues until the required number of terms is printed.
#include <stdio.h>
// Recursive function to print the fibonacci series
void fib(int n, int prev1, int prev2) {
// Base Case: when n gets less than 3
if (n < 3) {
return;
}
int curr = prev1 + prev2;
printf("%d ", curr);
return fib(n - 1, prev2, curr);
}
// Function that handles the first two terms and calls the recursive function
void printFib(int n) {
// When the number of terms is less than 1
if (n < 1) {
printf("Invalid number of terms\n");
}
// When the number of terms is 1
else if (n == 1) {
printf("%d ", 0);
}
// When the number of terms is 2
else if (n == 2) {
printf("%d %d", 0, 1);
}
// When number of terms greater than 2
else {
printf("%d %d ", 0, 1);
fib(n, 0, 1);
}
return;
}
int main() {
int n = 9;
// Printing first 9 Fibonacci series terms
printFib(n);
return 0;
}
Output
0 1 1 2 3 5 8 13 21
Explanation
- printFib() handles the first two terms, 0 and 1, and calls fib() for the remaining terms.
- fib() calculates the next term using prev1 + prev2 and recursively updates the previous two terms.
- The recursion stops when n becomes less than 3.
- main() calls printFib() to print the first n Fibonacci terms.