// ex_ch9_10.c
#include <stdio.h>

long Fibonacci(int n);

int main(void)
{
	int i;
	
	for (i = 1; i <= 7; i++)
	{
		printf("%ld\n", Fibonacci(i));
	}
	
	return 0;
}

long Fibonacci(int n)
{
	long result;
	// pre-initialized values for the first and second value
	int last = 1, secondlast = 1;
	int i;
	
	switch (n)
	{
	case 1:
	case 2:
		result = 1;
		break;
	default:
		// calculate the rest in a loop
		for (i = 3; i <= n; i++)
		{
			// the result is the addition of the last 2 values
			result = last + secondlast;
			/* shift the values: the last to the second last
			   and the result to the last */
			secondlast = last;
			last = result;
		}
	}
	
	return result;
}
