C Program to print Fibonacci series

For a better understanding of this C Fibonacci program example, we always recommend you to learn the basic topics of C programming listed below:

What is a Fibonacci series?

In this C tutorial, we will learn the ways to display the Fibonacci series in the C programming language. Fibonacci series are numbers generated by adding a number with the previous number of that number. Here we have to generate a Fibonacci series of n numbers using C. In other words, the Fibonacci series is a sequence where the next term is the sum of two previous terms.

The first two numbers in the Fibonacci series are either 1 and 1, or 0 and 1, depending on the chosen starting point of the series, and each number is the sum of the previous two numbers. The mathematical formula to find the Fibonacci series is given below f_n = f_{n-1} + f_{n-2},\!\, first two numbers are 0 and 1 that we have to give manually, then 0+1 is 1, now add 1+1 is 2 and so on.

How to print Fibonacci series using C program

To solve this C program we need to import the header libraries and then we accept the number of terms needed in the Fibonacci series from the user, we hard code the first two values in the series as 0 and 1 and initialize a variable count as 2 because the first two values we already printed. 

Now we use a while loop from count to the number of terms entered from the user and we calculate the next number in the series by adding the previous two numbers. And increment the count by one. And print the Fibonacci series. 

ALGORITHM

STEP 1: Import the header libraries to the program to use the built-in functions.

STEP 2: Start the program execution by using the main() function.

STEP 3: Define the variables using int datatype and initialize fib1 and fib2 as 0 and 1.

STEP 4: Accept the number of terms needed in the Fibonacci Series from the user using printf and scanf functions.

STEP 5: Print the first two terms of the series as 0 and 1 then make the Count as 2

STEP 6: Use the while loop from 2 to the number of terms to print all the Fibonacci Series. 

STEP 7: Calculate the third term by Adding the first two terms and Increment the Count by 1

STEP 8: Print the term of the series.

STEP 9: Swap the numbers of fib1 as fib2 and fib2 as fib3.

C Source Code

                                          #include <stdio.h>

void main() {
  int fib1 = 0, fib2 = 1, fib3, N, count = 0;
  printf("Enter the value of N\n"); /* enter the value from the user  */
  scanf("%d", & N);
  printf("First ?IBONACCI numbers are ...\n", N);
  printf("%d\n", fib1);
  printf("%d\n", fib2);
  count = 2; /* fib1 and fib2 are already used */
  while (count < N) {
    fib3 = fib1 + fib2; /* while loop for generating first N Fibonacci numbers  */
    count++;
    printf("%d\n", fib3); /* display the Fibonacci series  */
    fib1 = fib2;
    fib2 = fib3; /* changing the variables */
  }
} /* End of main() */
                                      

OUTPUT

Enter the value of N
10

First 5 FIBONACCI numbers are ...

0
1
1
2
3
5
8
13
21
34