C Program to print Fibonacci series


March 18, 2022, Learn eTutorial
1297

Fibonacci series is a series of numbers where each number will be the sum of two adjacent preceding numbers. 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 program, we will learn how to display the Fibonacci series. Fibonacci series are numbers generated by adding two preceding numbers of the calculated number.

For example, let us check the Fibonacci series 0, 1, 1, 2, 3, 5, . . .  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. 1+1 =2, then 2+3 = 5, and so on.

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.

In simple words, the Fibonacci series is a sequence where the next term is the sum of two previous terms.

Prime number Checking

How to print the Fibonacci series using the C program

To solve this Fibonacci series C program, we need to import the header libraries and then accept the number of terms needed in the Fibonacci series from the user, Then we print hardcoded 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 by the user and calculate the next number in the series by adding the previous two numbers which we stored in two variables fib1 and fib2 and print the resultant number. Now increment the variable count by 1 and then change the variable values of fib1 as fib2 and fib2 as the resultant number for finding the next value in the next iteration of the loop. 

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 FIBONACCI 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