Write a c program to print fibonacci series without using recursion and using recursion.

Ram Pothuraju

Using Iteration


 #include <stdio.h>


int main()

{

    int n, i, t1 = 0, t2 = 1, nextTerm;

    printf("Enter the number of terms: ");

    scanf("%d", &n);


    printf("Fibonacci Series: ");


    for (i = 1; i <= n; ++i)

    {

        printf("%d, ", t1);

        nextTerm = t1 + t2;

        t1 = t2;

        t2 = nextTerm;

    }


    return 0;

}


Using Recursion


#include <stdio.h>


int fibonacci(int n);


int main()

{

    int n, i;

    printf("Enter the number of terms: ");

    scanf("%d", &n);


    printf("Fibonacci Series: ");


    for (i = 0; i < n; ++i)

    {

        printf("%d, ", fibonacci(i));

    }


    return 0;

}


int fibonacci(int n)

{

    if (n == 0 || n == 1)

    {

        return n;

    }

    else

    {

        return (fibonacci(n-1) + fibonacci(n-2));

    }

}



Tags

Post a Comment

0Comments

Post a Comment (0)