1.
What is Fibonacci Series?
2.
Program To Print Fibonacci Series in C Using Recursion
2.1.
Program to find fibonacci series in c using recursion:
3.
Program To Print Fibonacci Series in C without Using Recursion
3.1.
Program to find fibonacci series in c without using recursion:
3.2.

4.
4.1.
Why do we use Fibonacci series?
4.2.
How to find Fibonacci series in C using function?
4.3.
What is Fibonacci series in C using while loop?
4.4.
What are the 10 Fibonacci numbers?
5.
Conclusion
Last Updated: Apr 28, 2024
Easy

# Fibonacci Series Program in C

Gaurav Joshi
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

## What is Fibonacci Series?

Fibonacci Series in C:- 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,5,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 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 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## 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  numbers are printed.

You can practice by yourself with the help of an online C compiler.

### Why do we use Fibonacci series?

Fibonacci series is an example of a self-synchronizing code, making it easier to recover data from a damaged stream.

### How to find Fibonacci series in C using function?

Create a function that calculates and displays Fibonacci numbers one at a time. Then, add the previous two numbers together and call the function to print the Fibonacci series.

### What is Fibonacci series in C using while loop?

Each number in the Fibonacci series is equals to the sum of the two numbers before it in the C language when using a while loop. Until a particular condition is satisfied, the while loop iteratively calculates and prints Fibonacci numbers.

### What are the 10 Fibonacci numbers?

The first 10 Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. In the series, each number is the sum of the two numbers before it.

## Conclusion

In this article, we talk about the Fibonacci series. We use various methods to explain how to print the Fibonacci series in C language. We also explore its execution using recursion and without recursion.

You can also check out Fibonacci Series in Python just to be thorough.

Recommended Articles

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Code 360.

Live masterclass