The following code shows how to display Fibonacci Series in C Language. A fibonacci series is the one whose next term equals the sum of previous two terms. The following code shows fibonacci series in two different ways. While, the first function shows the series of specified number of terms. Another function takes a value as a parameter. Then, it displays the series till the last term remains less than or equal to the specified value.

// Display Terms of the Fibonacci Series
#include <stdio.h>
void fib1(int n)
{
    int x=-1, y=1, z, i;
    printf("\nFibonacci Series of %d Terms...\n", n);
    for(i=1;i<=n;i++)
    {
        z=x+y;
        x=y;
        y=z;
        printf("%d ", z);
    }
}
void fib2(int n)
{
    int x=-1, y=1, z, i;
    printf("\n\nFibonacci Series till %d...\n", n);
    while(1)
    {
        z=x+y;
        if(z>n)
            break;
        x=y;
        y=z;
        printf("%d ", z);
    }
}
int main()
{
    int number_of_terms, last_term;
    printf("Enter the Number of Terms in Fibonacci Series: ");
    scanf("%d", &number_of_terms);
    fib1(number_of_terms);
    printf("\nEnter the value of Last Term in Fibonacci Series: ");
    scanf("%d", &last_term);
    fib2(last_term);
    return 0;
}

Output

Enter the Number of Terms in Fibonacci Series: 20

Fibonacci Series of 20 Terms…
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Enter the value of Last Term in Fibonacci Series: 700

Fibonacci Series till 700…
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610


Further Reading

50+ C Programming Interview Questions and Answers

C Program to Find Factorial of a Number