Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89 ...

#include<stdio.h>
main()
{

static int prev_number=0, number=1; // static: so value is not lost

int fibonacci (int prev_number, int number);

printf ("Following are the first 25 Numbers of the Fibonacci Series:\n");

printf ("1 "); //to avoid complexity

fibonacci (prev_number,number);


}



fibonacci (int prev_number, int number)

{
static int i=1; //i is not 0, cuz 1 is already counted in main.
int fibo;


if (i==25)
{
printf ("\ndone"); //stop after 25 numbers

}

else
{
fibo=prev_number+number;
prev_number=number; //important steps

number=fibo;

printf ("\n%d", fibo);
i++; // increment counter

fibonacci (prev_number,number); //recursion

}

}

3 comments:

  1. #include
    void sequence(int,int,int);

    void main()
    {
    int a=1,b=1,c=1;

    sequence(a,b,c);

    }


    void sequence(int x,int y,int z)
    {

    if(x==1)
    {
    printf("%d\n",x);
    printf("%d\n",x);
    }

    if(z<=23)
    {
    printf("%d\n",x+y);
    sequence(x+y,x,z+1);
    }

    }

    ReplyDelete

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin