C Program for Fibonacci Series

Fibonacci series

Fibonacci series is a series where the next term is the sum of the previous two terms. The first two terms of the Fibonacci series are 0 and 1.

For example, 0 and 1 are first two terms, then the third term is 0 + 1 = 1, fouth term is 1 + 1 = 2 and so on.

Let's write a C Program to find Fibonacci series.

Program


#include <stdio.h>

int main()
{
    int term1 = 0, term2 = 1;
    int nextTerm = term1 + term2;
    int num, i;

    //Number of terms to find Fibonacci series
    printf("Enter number of terms : ");
    scanf("%d", &num);

    //Print the first two terms
    printf("Fibonacci series is %d %d ", term1, term2);

    //Print third to nth terms
    for(i=3; i<=num; ++i)
    {
        printf("%d ", nextTerm);
        term1 = term2;
        term2 = nextTerm;
        nextTerm = term1 + term2;
    }
    return 0;
}

Output 1


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

Output 2


Enter number of terms : 17
Fibonacci series is 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987

Post a Comment

0 Comments