Return to Blog entry
This is the initial solution maybe I can do better than this. I remember back in college I used to solve the Fibonacci series using recursion. But this one tries to keep it simple without using any recursion. The first 2 numbers fib1 and fib2 can be changed when invoking the program so you can use either 0 0 or 0 1.
#include <stdio.h> // running the program // ./a.out 0 1
int main(int argc, char *argv[]) { int fib1, fib2, fibNext, max, i; printf("Number of Fibonacci series? "); scanf("%d", &maxNum);
fib1=atoi(argv[ 1 ]);
fib2=atoi(argv[ 2 ]);
fibNext = fib1 + fib2;
printf("%d, %d, %d, ", fib1, fib2, fibNext);
for(i=1;i<=max-3;++i){ fib1 = fib2; fib2 = fibNext; fibNext = fib1 + fib2; printf("%d, ", fibNext); } return 0; }
scanf("%d", &maxNum);
It should be just max and not maxNum