fibonacci series


See On Github

Data

Contributor

Generic placeholder thumbnail

by harshasrisri

in c

Tags

recursion

Source Code

fibo
====

fibo.c:
	a usual implementation where a function is called and it prints 'count' number of elements of Fibonacci series that is passed as the argument

fibo_r.c:
	a "reentrant" C implementation to return successive elements on repetitive calls
#include <stdio.h>

void fibo (int count) {
	int a = 0, b = 1; count -= 2;
	printf ("%d\n", a); printf ("%d\n", b);
	while (count--) printf ("%d\n", (b>a) ? (a += b) : (b += a));
}

int main (int argc, char **argv) { 
	fibo (argv[1] ? atoi(argv[1]) : 10); 
}
#include<stdio.h>

int fibo_r () {
	static int a = 0, b = 1, count = 0;

	if (0 == count) { count++; return a; }
	if (1 == count) { count++; return b; }
	return (b>a) ? (a += b) : (b += a);
}

int main (int argc, char **argv) {
	int count = 20;
	if (argv[1]) count = atoi(argv[1]);
	while (count--) printf ("%d\n", fibo_r());
}