Learn about C program to display first n numbers in the Fibonacci series using recursion in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to display first n numbers in the Fibonacci series using recursion in C
Program:
#include<stdio.h>
int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
int main()
{
int num;
printf("How many numbers of Fibonacci series you want to print: ");
scanf("%d",&num);
printf("Fibonacci series:\n");
for(int i=0; i<num; i++)
{
printf("%d\t",fibonacci(i));
}
return 0;
}
Output:
How many numbers of Fibonacci series you want to print: 5
Fibonacci series:
0 1 1 2 3
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to Find Nth Fibonacci term using Recursion
C program to find prime factors of a number using recursion techniques