Fibonacci Series C Program

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers. The first two numbers in the sequence are always 0 and 1. In this article, we will discuss how to write a C program to generate the Fibonacci series.

To generate the Fibonacci series in C, we will use a loop to iterate through the sequence and calculate each number in turn. Here is the complete code for a C program that generates the Fibonacci series:

Print Fibonacci Series C Program

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

Let’s break down this code line by line.

  • The first line includes the standard input/output library stdio.h, which provides the printf() and scanf() functions.
  • The main() function is the entry point for the program.
  • Inside the main() function, we declare four integer variables: n for the number of terms to generate, i for the loop counter, t1 and t2 for the first two terms in the series, both initialized to 0 and 1, respectively.
  • We prompt the user to enter the number of terms to generate using the printf() and scanf() functions.
  • We output the first term of the series using the printf() function.
  • We then use a for loop to iterate through the series and calculate each term in turn.
  • Inside the loop, we calculate the next term in the series by adding t1 and t2 together and storing the result in nextTerm.
  • We update t1 to be equal to t2 and t2 to be equal to nextTerm.
  • We output each term in the series using the printf() function, separated by a comma and a space.
  • Once the loop is finished, we return 0 to indicate that the program executed successfully.

Input & Output

When we run this program and enter a value of 10 for the number of terms to generate, the output will be:

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

This program demonstrates how to generate the Fibonacci series in C using a loop. The Fibonacci series is a simple but important mathematical concept that is used in many different programming tasks. By understanding the basics of C syntax and loops, you can create more complex programs that perform a wide variety of tasks.