C Program to print the Fibonacci Series upto n terms



#include<stdio.h>
int main()
{
  int firstterm=0,secondterm=1;
  int nextterm,i,n;
  printf("Enter the range of the series\n");
  scanf("%d",&n);
  printf("Fibonacci series is:\t");
  if(n==1)
    printf("%d\t",firstterm);
  else if(n==2)
    printf("%d\t%d\t",firstterm,secondterm);
  else
  {
      printf("%d\t%d\t",firstterm,secondterm);
      for(i=2;i<n;i++)
      {
          nextterm=firstterm+secondterm;
          printf("%d\t",nextterm);
          firstterm=secondterm;
          secondterm=nextterm;
      }
  }
}

Output:

C Program to print the Fibonacci Series upto n terms


Comments