Let us now write a program that uses a function Fib() to compute the first n terms of the series.
/* This program generates Fibonacci series*/
#include<stdio.h>
int fib(int);
void main()
{
int n;
int i,term;
printf( "\nEnter the terms to be generated: ") ;
scanf ("%d", &n);
for(i=1;i<=n;i++)
{
term = fib(i);
printf("%d ", term);
}
}
// Function to return a fibonacci term
int fib(int n)
{
if (n==1)
return 0;
else
if (n==2)
return 1;
else
return(fib(n - 1) + fib(n - 2));
}
From above, we can summarize the characteristics of recursion as:
nThere must exist at least a basic solution called terminating condition. This statement ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.