0
875views
What is recursion? Write a program to compute fibonacci series using recursion.
1 Answer
2
27views
written 3.0 years ago by |
Recursion is a widely used phenomenon in computer science used to solve complex problems by breaking them down into simpler ones. Recursion is a process by which a function calls itself directly or indirectly. The corresponding function is called as recursive function. Using recursive algorithms, certain complex problems can be solved quite easily.
In a recursive function, the solution to the base case is provided and the solution of the bigger problem is expressed in terms of smaller problems. The role of the base condition is to stop a recursive function from executing endlessly – once a pre-specified base condition is met, the function knows it’s time to exit.
Program to compute Fibonacci series using recursion
#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
ADD COMMENT
EDIT
Please log in to add an answer.