fibonacchi series in c

Fibonacci Series in C

Here is an example of how to generate the Fibonacci series in C:

#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;
}

Explanation

  1. The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.

  2. The main function is defined, which is the entry point of the program.

  3. Inside the main function, the following variables are declared:

  4. n: The number of terms in the Fibonacci series.
  5. i: A loop counter.
  6. t1 and t2: The first two terms of the series.
  7. nextTerm: The next term in the series.

  8. The user is prompted to enter the number of terms they want in the Fibonacci series using printf and scanf.

  9. The program then prints the message "Fibonacci Series: " using printf.

  10. A for loop is used to generate the Fibonacci series. The loop runs from i = 1 to i <= n, incrementing i by 1 in each iteration.

  11. Inside the loop, the current term t1 is printed using printf.

  12. The next term nextTerm is calculated by adding the previous two terms t1 and t2.

  13. The values of t1 and t2 are updated for the next iteration. t1 is assigned the value of t2, and t2 is assigned the value of nextTerm.

  14. After the loop finishes, the main function returns 0, indicating successful execution of the program.

This program generates the Fibonacci series up to the specified number of terms entered by the user. Each term is calculated by adding the previous two terms. The initial terms t1 and t2 are set to 0 and 1, respectively, and the subsequent terms are calculated using a loop.