Tuesday, 12 October 2010

28. Program to generate Fibonacci Series

The Fibonacci series :

0, 1, 1, 2, 3, 5, 8, 13, 21, …..


Fibonacci series begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms. Now, I want to write a C program for print the fibonacci series.


#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
   int OldNum, NewNum, FibNum, MaxNum;
   printf("Generate Fibonacci Numbers till what number? ");
   scanf("%d", &MaxNum);
   OldNum=0;
   NewNum=1;
   FibNum = OldNum + NewNum;
   printf("%d, %d, %d, ", OldNum, NewNum, FibNum);
   for(;;)
   {
      OldNum = NewNum;
      NewNum = FibNum;
      FibNum = OldNum + NewNum;
      if(FibNum > MaxNum)
      {
         printf("");
         exit(1);
      }
      printf("%d, ", FibNum);
   }
}
post by arnob.

No comments:

Post a Comment