AD1

Tuesday, 15 November 2016

Write a recursive function to print the first 25 elements of fibonacci series 

fibonacci series is characterized by the fact that every number after the first two is the sum of the two preceding ones
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots \;

solution

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
void fibonacci25(int start,int current)
{
int next;
static int t=23;
if(t--)
{
next=start+current;
start=current;
current=next;
printf("%d\n",next);
fibonacci25(start,current);
}
return;
}
int main()
{
int start=0; int current=1;
printf("%d\n%d\n",start,current);//print first two terms 0,1
fibonacci25(start,current);//function that print first 25 number of fibonacci series
return 0;
}
view raw fiborecursive.c hosted with ❤ by GitHub

No comments:

Post a Comment