Thursday, 27 October 2011

C program for Fibonacci series

WRITE A C PROGRAM TO GENERATE THE FOLLOWING FIBONACCI SERIES



#include<stdio.h>
#include<conio.h>
void main()
{
  int n,a=1,b=1,c,i;
  printf("Enter the number of terms you want to display:");
  scanf("%d",&n);
  if(n==1)
   printf("Fibonacci series: %d", a);
  else
   printf("Fibonacci series:");
   printf("%d%d",a,b);
  for(i=1;i<=(n-2);i++)
 {
   c=a+b;
   printf("%d ",c);
   a=b;
   b=c;
  }
  getch();
}


Output:
   Enter the number of terms you want to display:3
   Fibonacci series:1 1 2

No comments:

Post a Comment