Robson » Code » C++ » Fibonacci

 
// fibonacci sequence viewer v1.0
 
// note: this needs some improvements:
// - proper usage instructions
// - handling larger numbers
// - better handling when 1 is requested
 
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
   int iMax = atoi(argv[1]);
   int fibonacci[iMax];
   int iCurrent = 0, iCounter = 2;
   fibonacci[0] = 0;
   fibonacci[1] = 1;
   printf("0, 1");
   while (iCounter < iMax)
   {
       iCurrent = fibonacci[iCounter-1] + fibonacci[iCounter-2];
       fibonacci[iCounter++] = iCurrent;
       printf(", %d", iCurrent);
   }    
}
 
© 2004-17 robson | cc unless stated