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.

Recommended Answers

All 5 Replies

#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.

i hope it's not iteration and it's recursion

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 ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.