Skip to content
Home » Blog » C Program to find fibonacci series

C Program to find fibonacci series

C fibonacci series

In this article, you’ll learn about C Fibonacci series, how it works and how can we use it.

What are C Fibonacci series and Fibonacci series logic?

A series of numbers in which each sequent number is the sum of its two previous numbers is known as Fibonacci series and each number is called Fibonacci numbers.

Algorithm

Algorithm for Fibonacci series. Fn = Fn-2 + Fn-1.

To understand this example, you should have the knowledge of following C programming topic:

  • for Loop
  • while and do…while Loop

First, two numbers of the Fibonacci series is 0 and 1. From 3rd number onwards, the series will be the sum of previous 2 numbers.

Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, etc

Program to find Fibonacci Series

#include <stdio.h>
int main()
{
    int i, n, 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;
}

Output:

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

C Program For Fibonacci Series

Program To Generate Fibonacci Sequence Up To A Certain Number

#include <stdio.h>
int main()
{
    int t1 = 0, t2 = 1, nextTerm = 0, n;

    printf("Enter a positive number: ");
    scanf("%d", &n);

    // displays the first two terms which is always 0 and 1
    printf("Fibonacci Series: %d, %d, ", t1, t2);

    nextTerm = t1 + t2;

    while(nextTerm <= n)
    {
        printf("%d, ",nextTerm);
        t1 = t2;
        t2 = nextTerm;
        nextTerm = t1 + t2;
    }
    
    return 0;
}

Output

Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

C Program To Generate Fibonacci Sequence Up To A Certain Number

Related C Programs

Ask your questions and clarify your/others doubts on Fibonacci series by commenting. Documentation