×

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

Data Types in C

Data type is a very important concept in C programming language. Simply, “A data type is the classification of data values that a data item can have.” We need to...

11 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Sizeof in C

A pointer in C is defined as a variable that stores the information of the address of another variable. A pointer can be incremented or decremented, which means we can...

4 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

What is Linked List in C

Linked list Similar to an array, a linked list is a linear data structure that stores a chain of nodes with two fields of memory in each node. Where first memory...

7 minutes read.