×

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language.

What does the term "Fibonacci Series" mean?

The following number in a Fibonacci number series is essentially the summation of the previous two values (preceding numbers). In every series, the first two integers are still 0 and 1. Thus, the sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, so on.

How to write the Fibonacci Sequence in C?

Two of the main approaches that can be used to write this series in C language:

  • Using Recursion (Only this approach is discussed in this tutorial)
  • Without Recursion

Fibonacci Series using Recursion:

Let's look at a recursive Program for the Fibonacci series in C Language

Code:

C Program to write Fibonacci Series

#include<stdio.h>


void printFibonacciseries(int p)
 {


static int p1=0, p2=1, p3; //considering variables


if( p > 0 ) //if condition is true it will execute
{
//logic


p3 = p1 + p2; 


p1 = p2;


p2 = p3;


printf("%d ", p3); //printing value of the series


printFibonacciseries( p - 1) ; //user-defined function


}


 }


int main()
{


int p; //considering variable


printf("Enter any number to create Fibonacci Series: "); //asks user for a input


scanf("%d", & p); //storing value in variable p


printf("The Fibonacci Series should be: "); //printing statement


printf("%d %d ",0,1); //printing 0 and 1 as these are common for all fibonacci series


printFibonacciseries( p - 2 ); //using p-2 as we have already printed two numbers here 


return 0;


}

Output: (If user puts 11 as input)

Enter any number to create Fibonacci Series: 11
The Fibonacci Series should be: 0 1 1 2 3 5 8 13 21 34 5

Output: (If user puts 9 as input)

Enter any number to create Fibonacci Series: 9
The Fibonacci Series should be: 0 1 1 2 3 5 8 13 21

Related Topics

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Union Program in C

 How should a union be defined?  In C programming, a Union is a method used to organize different data types into groups in the same way that Structures are used. We...

4 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Multiplication Table in C

Displaying table up to 10 #include <stdio.h> int main() {     int num, i = 1;     printf("     Enter any Number:");     scanf("%d", &num);     printf("Multiplication table of %d: ", num);  ...

1 minute read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.