// july24ex2.cpp : Defines the entry point for the console application.
// Fibonacci sequence - Solved interatively

#include "stdafx.h"

long fib_iter (int);
long fib_recur (int);

int main(int argc, char* argv[])
{    int term, final_term;
      long fib, fib2;

	  printf("What term of the fib seq would you like to print?? ");
	  scanf("%d", &final_term);

      for (term=0; term <= final_term; term++){
		  fib= fib_iter (term);
		  fib2 = fib_recur (term);
		  printf("Term %3d is = %ld %ld\n", term, fib, fib2);
	  }  /* end for term  */
	
	
	return 0;
}   /* end main */


long fib_iter (int term) {
   long second_last=0, last=1, current;
   int which_term;

    if (term==0) return 0;
	if (term==1) return 1;
    for (which_term = 2; which_term<= term; which_term++) {
        current = second_last + last;

		second_last = last;
		last = current;

	}  /* end for which_term */



     return current;

} /* end fib_iter */


long fib_recur (int term) {

	if ( (term==0) || (term==1) )
		return term;
	 else
	    return fib_recur (term - 1) + fib_recur (term - 2); 

} /* end recursive function fib_recur */