Fibonacci series is a series of numbers in which the next number is the sum of the previous two numbers for eg: 0,1,1,2,3,8,13,21 etc.
The first Fibonacci numbers are 0 and 1 respectively
F(N) = F(N - 1) + F(N - 2)
The above expression is the general equation followed by every Fibonacci number.
Two main approaches for printing the Fibonacci series in C are-
- Fibonacci Series using recursion
- Fibonacci Series without recursion
Program To Print Fibonacci Series in C Using Recursion
We can get the Fibonacci series in C using the recursive approach. To print the Fibonacci series in C language using recursion. We can define a recursive function to perform this task.
We can define a recursive function that takes an integer n as an argument and returns the nth Fibonacci number.
In the recursive function, the base conditions are when n is either 0 or 1, representing the first and second terms of the Fibonacci series. Any other tern is calculated by making a recursive call for the lower values since the nth Fibonacci numbers depend upon the value of (n-1)th and (n-2)th Fibonacci term.
Program to find fibonacci series in c using recursion:
// Including Header Files
#include<stdio.h>
//Recursive function to get the nth Fibonacci number
int nthFibonacci(int n){
// Base Condition
if(n<=1){
return n;
}
// Using the relation- F(N)=F(N-1)+F(N-2)
return nthFibonacci(n-1)+nthFibonacci(n-2);
}
int main() {
// The number of terms in the Fibonacci Series we want to output
int n;
printf("Enter the numbers of terms in the Fibonacci Series\n");
scanf("%d",&n);
printf("The first %d numbers in the Fibonacci Series are \n",n);
//Running a for loop for n times.
for(int i=0;i<n;i++){
//Storing the (i+1)th Fibonacci term
int curr=nthFibonacci(i);
printf("%d ",curr);
}
}
Output
Enter the numbers of terms in the Fibonacci Series
10
The first 10 numbers in the Fibonacci Series are
0 1 1 2 3 5 8 13 21 34
Explanation
We define a recursive function nthFibonacci, it takes an integer n as an argument and returns the nth Fibonacci number. We take input from the user and store it in variable n. We make this function call inside a for loop which runs for n times. In the ith iteration, the nthFibonacci function will return the ith Fibonacci term.
In the recursive function, the base conditions are when n is either 0 or 1, representing the first and second terms of the Fibonacci series. Any other tern is calculated by making a recursive call for the lower values since the nth Fibonacci numbers depend upon the value of (n-1)th and (n-2)th Fibonacci term.
Program To Print Fibonacci Series in C without Using Recursion
We can get the Fibonacci series in C using a non-recursive approach such as a For loop, While loop, or user-defined functions.
We can print the Fibonacci series in an iterative way by using the for loop in C. We already know that for loop runs a certain block of code until a condition is satisfied. This method uses the formula that is provided to us. We will run a for loop for n times to print the first n numbers of a Fibonacci series.
We keep a count of the ith Fibonacci number and the (i+1)th Fibonacci number for the ith iteration. In the ith iteration, we are going to print the ith Fibonacci number. Then we update the values of the variables storing the current Fibonacci numbers. The ith Fibonacci number becomes the (i+1)th Fibonacci and we store and calculate the next Fibonacci number in the variable storing the (i+1)th Fibonacci number.
Program to find fibonacci series in c without using recursion:
// Including Header Files
#include<stdio.h>
int main() {
// The number of terms in the Fibonacci Series we want to output
int n;
printf("Enter the numbers of terms in the Fibonacci Series \n");
scanf("%d",&n);
// Defining the first and second terms in the Fibonacci Series
int f1=0,f2=1;
printf("The first %d numbers in the Fibonacci Series are \n",n);
for(int i=1;i<=n;i++){
//Printing the ith Fibonacci number
printf("%d ",first);
//Storing the (i+2)th Fibonacci number in f3
int f3=f1+f2;
// updating f1
f1=f2;
// Updating f2
f2=f3;
}
}
Output
Enter the numbers of terms in the Fibonacci Series
10
The first 10 numbers in the Fibonacci Series are
0 1 1 2 3 5 8 13 21 34
Explanation
We first take input from the user defining the number of terms we want to print and storing it in variable n. We run the for loop for n times. The variables f1 and f2 store the first and second Fibonacci numbers of the series.
Then, we run loop n times depending upon the user’s input. We print the first n Fibonacci number. Then we calculate and store the Fibonacci number that will be coming in the Fibonacci series after the number stored in a variable f3 using the formula F(N) = F(N - 1) + F(N - 2). Then we update the value of the variable f1 with the value stored in the variable f2 and the value of the variable f2 with f3. And in this way, for every iteration i, the variable f1 will store the ith Fibonacci number and the variable f2 will store the (i+1)th Fibonacci numbers. The for loop ends when n numbers are printed.
You can practice by yourself with the help of an online C compiler.