C program to Print Fibonacci series

Write a program in the C programming language to print Fibonacci series up to the inputted number by using for loop statement. Below is the solution of the C program –

/* C program to print the fibonacci series*/
#include<stdio.h>
main()
{
     int f1,i,f2,f3,n;
      f1=0;
      f2=1;

      printf("upto which no.you want series");
      scanf("%d",&n);
      printf("%d \t %d \t",f1,f2);
      for(i=2;i<n;i++)
      {
              f3=f1+f2;
              f1=f2;
              f2=f3;
              printf("%d\t",f3);
      }
      getch();
}

Description – For loop is used to print the series up to n. First two terms of the Fibonacci series are initialized in the compile time initialization and from the first two terms the rest of the terms are created. After creating the new term ‘ f3 ‘; it is printed. The value of the new term and it’s previous term is stored in ‘ f2 ‘ and ‘ f1 ‘ so that the next new term can be created from these variables.
Input – 12
Output – 0   1   1   2   3   5   8   13   21   34   55   89

yashan has written 69 articles

2 thoughts on “C program to Print Fibonacci series

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>