Fibonacci Series in C

ure! Here’s an example of how you can generate the Fibonacci series in C:

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (int i = 0; i < n; i++) {
        if (i <= 1)
            next = i;
        else {
            next = first + second;
            first = second;
            second = next;
        }
        printf("%d ", next);
    }

    return 0;
}

In this code, we first ask the user to input the number of terms they want in the Fibonacci series. Then, using a for loop, we generate each term of the series and print it. We initialize the first term as 0 and the second term as 1. For each subsequent term, we calculate it by adding the previous two terms.

For example, if the user enters 10 as the number of terms, the output will be:

Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

Fibonacci Series in C without recursion:

Certainly! Here’s an alternative implementation of the Fibonacci series in C without using recursion:

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", first);
        next = first + second;
        first = second;
        second = next;
    }

    return 0;
}

In this code, we use a for loop to iterate and calculate each term of the Fibonacci series. We initialize the first term as 0 and the second term as 1. Inside the loop, we print the current term (first), then calculate the next term by adding the previous two terms (first and second). Finally, we update first and second for the next iteration.

Using this approach, the program will output the Fibonacci series up to the specified number of terms.

For example, if the user enters 10 as the number of terms, the output will be:

Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

Fibonacci Series using recursion in C:

Certainly! Here’s an example of how you can generate the Fibonacci series using recursion in C:

#include <stdio.h>

int fibonacci(int n);

int main() {
    int n;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (int i = 0; i < n; i++) {
        printf("%d ", fibonacci(i));
    }

    return 0;
}

int fibonacci(int n) {
    if (n <= 1)
        return n;
    else
        return fibonacci(n - 1) + fibonacci(n - 2);
}

In this code, we have a fibonacci function that calculates the Fibonacci number for a given index n. The function uses recursion to calculate the Fibonacci number by adding the two previous Fibonacci numbers (n-1 and n-2). The base case is when n is 0 or 1, in which case the function simply returns n.

In the main function, we ask the user to input the number of terms they want in the Fibonacci series. Then, using a for loop, we iterate from 0 to n-1 and call the fibonacci function for each index, printing the result.

For example, if the user enters 10 as the number of terms, the output will be:

Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34