In this article, we will discuss the Fibonacci series and the programs to print the Fibonacci series in C using recursion or loops.
What is Fibonacci Series?
The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the Fibonacci series.
In mathematical terms, the number at the nth position can be represented by:
Fn = Fn-1 + Fn-2
with seed values, F0 = 0 and F1 = 1.
For example, Fibonacci series upto 10 terms is: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

The Fibonacci programs in C that print the first n terms of the series can be coded using two methods specified below:
- Fibonacci series using recursion
- Fibonacci series using loops
Fibonacci Series Program in C (Recursion)
In this method, we will use a function that prints the first two terms, and the rest of the terms are then handled by the other function that makes use of a recursive technique to print the next terms of the sequence.
C
#include <stdio.h>
int prev1 = 1;
int prev2 = 0;
void fib(int n)
{
if (n < 3) {
return;
}
int fn = prev1 + prev2;
prev2 = prev1;
prev1 = fn;
printf("%d ", fn);
return fib(n - 1);
}
void printFib(int n)
{
if (n < 1) {
printf("Invalid number of terms\n");
}
else if (n == 1) {
printf("%d ", 0);
}
else if (n == 2) {
printf("%d %d", 0, 1);
}
else {
printf("%d %d ", 0, 1);
fib(n);
}
return;
}
int main()
{
int n = 9;
printFib(n);
return 0;
}
|
Output
0 1 1 2 3 5 8 13 21
Complexity Analysis
Time complexity: O(n), It is because, for printing n terms, the fib() function will call itself recursively for (n – 2) times and each time it will take constant time.
Space Complexity: O(n), It is because, for each recursive call of the fib() function, a separate stack frame is created. For (n-2) calls, (n-2) stack frames are created with results in the O(n) space complexity.
Fibonacci Series Program in C (Iteration)
In this method, we use one of the C loops to iterate and print the current term. The first two terms, F1 and F2 are handled separately. After that, we use two variables to store the previous two terms and keep updating them as we move to print the next term.
C
#include <stdio.h>
void printFib(int n)
{
if (n < 1) {
printf("Invalid Number of terms\n");
return;
}
int prev1 = 1;
int prev2 = 0;
for (int i = 1; i <= n; i++) {
if (i > 2) {
int num = prev1 + prev2;
prev2 = prev1;
prev1 = num;
printf("%d ", num);
}
if (i == 1) {
printf("%d ", prev2);
}
if (i == 2) {
printf("%d ", prev1);
}
}
}
int main()
{
int n = 9;
printFib(n);
return 0;
}
|
Output
0 1 1 2 3 5 8 13 21
Complexity Analysis
Time Complexity: O(n), Because for n number for terms, the loop inside the printFib() function will be executed n times.
Space Complexity: O(1), Because we only used a few variables which don’t depends on the number of terms to be printed.
For more details, refer to the complete article – Program to Find and Print Nth Fibonacci Numbers
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
14 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...