Fibonacci Series In C Using Recursion
In this tutorial, we are going to see how to write a C program to display Fibonacci series using recursion.
There are two ways to display Fibonacci series of a given number, by using for loop, or by recursive function.
Fibonacci series is a sequence of integers of 0, 1, 2, 3, 5, 8…
The first two numbers are 0 and 1. All the other numbers are obtained by adding the two previous numbers. This means that the nth number is the sum of the (n-1)th and (n-2)th number.
Fibonacci Series In C Using Recursion
#include <stdio.h> int fibonacci(int n) { if (n == 0 || n == 1) return n; else return (fibonacci(n-1) + fibonacci(n-2)); } int main() { int nbr, i = 0, j; printf("Enter the number of terms: "); scanf("%d", &nbr); printf("The first %d terms of the Fibonacci series are: \n", nbr); for (j = 1; j <= nbr; j++) { printf("%d\n", fibonacci(i)); i++; } return 0; }
This example will print the Fibonacci sequence of 8.
Enter the number of terms: 8 The first 8 terms of the Fibonacci series are: 0 1 1 2 3 5 8 13