Browse by Domains

Fibonacci Series In C | C Program To Display Fibonacci Sequence

Let us help you understand what a Fibonacci Series in C is and how it is formed! 

The Fibonacci series is a sequence of numbers in which each can be generated by adding up the previous two numbers. The first six numbers in the Fibonacci series are 0, 1, 1, 2, 3, 5, and 8. 

The Fibonacci series is also the simplest example of a recursive problem. In this article, we will see what recursive programming is and how to write a Fibonacci program in the C language with and without recursion. 

What is the Fibonacci series? 

The Fibonacci sequence has many interesting properties, but we’ll focus on one in particular: the ratio of any two consecutive numbers in the sequence is approximately the same as the ratio of the next two numbers. 

For example, if you divide 5 by 3, you get 1.666666666666667. We also get 1.6 when 8 is divided by 5. As you can see, the ratios get closer and closer to 1.6 as you move down the sequence. 

Fibonacci Series in C

The first and second term always remains constant! So, one needs to declare them in the variables as shown in the program below. 

#include<stdio.h> 
int main() 
{ 
int term1 = 0, term2 = 1; 
int count,nextTerm; 
//Take the user input to print the series 
printf("Enter the number of terms:\n");
scanf("%d",&count); 
printf("The First %d terms of Fibonacci series are :\n",count); for (int j = 0 ; j < count ; j++ ) 
{ 
if ( j <= 1 ) 
nextTerm = j; 
else 
{ 
nextTerm = term1 + term2; 
term1 = term2; 
term2 = nextTerm; 
} 
printf("%d\n",nextTerm); 
} 
return 0; 
} 

Output:

Enter the number of terms: 8 
The First 8 terms of the Fibonacci series are : 
0 
1 
1 
2
3 
5 
8 
13 

Fibonacci Series using Recursion

Before we dive into the Fibonacci series using recursion, let us see what recursion is! 

What is Recursive Programming & why is it useful?

Recursive programming is a type of programming in which a function calls itself again and again. 

It can be used to solve complex problems by breaking them down into smaller and smaller sub-problems until they can be solved. This makes recursive programming a powerful tool for problem-solving. 

One famous example of recursive programming is the Fibonacci series. The Fibonacci series can be generated by adding the previous two numbers to the sequence. The first few numbers are: 0, 1, 1, 2, 3, 5, 8. Here in this program, the function called Fibonacci is called to demonstrate recursion.

#include<stdio.h> 
int fibonacci(int); 
int main() 
{ 
int count, a = 0; 
printf("Enter number of terms:"); 
scanf("%d",&count);
printf("\nFibonacci series:\n"); 
for (int j= 1 ; j <= count ; j++ ) 
{ 
printf("%d\n", fibonacci(a)); 
a++; 
} 
return 0; 
} 
int fibonacci(int num) 
{ 
if ( num == 0 ) 
return 0; 
else if ( num == 1 ) 
return 1; 
else 
return ( fibonacci(num-1) + fibonacci(num-2) ); 
} 

Output:

Enter number of terms: 6 
Fibonacci series: 
0 
1 
1
2 
3 
5

Fibonacci Series Used to Teach Recursive Programming

The Fibonacci series is the perfect example of recursive programming because it’s easy to understand and can be implemented very simply. 

The Fibonacci series can be used to teach three essential concepts of recursive programming: the recursive call, the base case, and the stopping condition. 

The recursive call is when a function calls itself. The base case is the point at which the function stops calling itself and returns a value. The stopping condition is a condition that determines when the function should stop calling itself. 

Fibonacci Sequence Up to a Certain Number

Let us now see how to write a program if one is expecting a Fibonacci series up to a certain number. 

#include <stdio.h> 
int main() { 
int term1 = 0, term2 = 1, nextTerm = 0, n; 
printf("Enter a positive number: "); 
scanf("%d", &n); 
// displays the first two terms which is always 0 and 1 
printf("Fibonacci Series is : %d, %d, ", term1, term2); 
nextTerm = term1 + term2; 
while (nextTerm <= n) { 
printf("%d, ", nextTerm); 
term1 = term2;
term2 = nextTerm; 
nextTerm = term1 + term2; 
} 
return 0; 
} 

Output:

Enter a positive number: 80 
Fibonacci Series is : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 

Applications of the Fibonacci Series

The Fibonacci sequence has a lot of practical applications, especially in the world of finance. 

Some of the most notable applications of the Fibonacci sequence are in the fields of botany, music, and architecture. In botany, Fibonacci numbers are often used to calculate the number of petals on a flower. In music, Fibonacci numbers can be used to create rhythms and chord progressions. And in architecture, Fibonacci numbers can be used to create harmonic proportions in buildings. 

Use of Fibonacci Series in Interviews

The Fibonacci series is great for showing off your recursive programming skills. You’ll definitely wow your interviewer if you can write code to generate the Fibonacci series. The Fibonacci series is a great interview question and also a popular programming challenge. 

Use of Fibonacci Series in Coding

The Fibonacci sequence can be used in coding to generate sequences of numbers. The sequence can also be used to generate fractals, which are patterns that are infinitely repeated. This can be useful for creating designs and patterns in your code. 

Conclusion

The Fibonacci series is a simple example of recursive programming. You will be better prepared to tackle more complex recursive algorithms by understanding how to generate the Fibonacci series programmatically. Recursion is a powerful programming technique, and with a bit of practice, you will be able to write efficient and easy code to read and understand. Interested in brushing up on your C Programming skills? Enroll for the free C Programming course here and start upskilling.

Also Read: Fibonacci Series in Python | Python Program for Fibonacci Numbers
Avatar photo
Great Learning
Great Learning's Blog covers the latest developments and innovations in technology that can be leveraged to build rewarding careers. You'll find career guides, tech tutorials and industry news to keep yourself updated with the fast-changing world of tech and business.

Leave a Comment

Your email address will not be published. Required fields are marked *

Great Learning Free Online Courses
Scroll to Top