i need help with the following:

make a program that would display the nth of the fibonnaci sequence.

1 1 2 3 5 8 13 21 34 55 ... n

implement it using both iteration and recursion.

#include<stdio.h>

int fib (int x, int y, int 2);

void main (void)
{

}

#include<stdio.h>

int fib (int x, int y, int z);

void main (void)
{
int x, y, z, d;
x=1; y=1;

printf("Enter a number: ");
scanf("%d", &z);
d=fib(x, y, z);
}

int fib (int x, int y, int z)
{
if(z==0)
return 1;

else if(z==1)
return 2;

else
return fib(x, y, z-1) + fib(x, y, z-2)
}

my friend got this one, i have how and why questions about the processes, ways and all that goes with it. hoping for response. thank you.

U are on the way but need more efforts ..

Check this may help

As you know fib series is obtained by adding previous numbers, You want recursion so function must call itself repeated to get serious
Here is function which does that

fib (int x, int y, int z)
{
        if(z==0)
        { 
                return ;
        }

        printf("%d\n",x);
        z--;
 // For next number in serious add current number (x+y) and exact previous
        fib(y,(x+y),z);
}

Just call the function once from main()

int main (void)
{
        int x, y, z;
        x=0; y=1;
        printf("Enter a number: ");
        scanf("%d", &z);
        fib(x, y, z);
        return(0);
}

You try for iteration method ..

Edited 5 Years Ago by Shankye: n/a

This article has been dead for over six months. Start a new discussion instead.