Skip to content

C program to Print Fibonacci series

by yashan on September 10th, 2011

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

2 Comments
  1. mahmud permalink

    thanks

  2. mahe permalink

    Nice and want some typical program

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS