C Program to Print the Fibonacci series

Fibonacci series is the series of numbers which is found by adding the two numbers before it. For example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

C program to display Fibonacci series is as follows.

#include<stdio.h>
int main()
{
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms n");
   scanf("%d",&n); 
   printf("First %d terms of Fibonacci series are : n",n);
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d t",next);
   }
return 0;
}

Here, the user enters the number of terms that is to be displayed and the number is assigned to variable n. The loop is started by initializing variable c to 0 and the loop is carried out as long as c is less than n. For the first two terms of the series, the value of c is printed as 0 and 1 as no two terms are present in front of the first two terms in the series for addition. Then for the third term, the two terms before the third term (i.e. first and second term) are added. For the fourth term, the two terms before the fourth term (i.e. second and third term) are added and so on.

The program can also be done in following way.

#include<stdio.h>
int main()
{
   int i=1, n, first=0, second=1, third;
   printf("Enter the number of terms n");
   scanf("n %d",&n);
   printf("%d t %d t", first, second);
   do
   {
       third=first+second;
       printf ("t %d t",third);
       first=second;
       second=third;
       i=i+1;
   } while (i<=n-2);
return 0;
}

The mechanism of this program is the same as the program above. But the difference is that in this program, the first two terms of the series is displayed outside the loop and the rest terms are displayed inside the loop. So this program does not work correctly if the user enters number of terms as 0, 1, or 2.

Output:

Enter the number of terms

9

First 5 terms of Fibonacci series are:

0              1              1              2              3              5              8              13

To Display Fibonacci Series until Last Term is less than 34

The logic or the mechanism to display the Fibonacci series until last term is less than 34 are the same as the program above. But the only difference here is that a keyword break has to be used when the term reaches 34. The keyword break exits from the loop and the next statement to be executed is the statement after the end of the loop.

C program to display Fibonacci series until last term is less than 34 is shown below

#include<stdio.h>
int main()
{
    int first=0, second=1, third, i=1;
    printf ("%d t %d", first, second);
    while (1)
    {
        third = first + second;
        if (third==34)
            break;
        printf ( "t %d t", third);
        first = second;
        second = third;
    }
    return 0;
}

Here, when the third term i.e. next term in the series is 34, without displaying 34 on the screen, the break; keyword exits the loops and the statement return 0; is executed.

Output:

0              1              1              2              3              5              8              13           21