Saturday, August 16, 2008

ISC 2005 Fibonacci Series through Recursion

Link for Downloading full program C++

VERSION I

int fib(int n)
  {
  if(n==1) return 0;
  else if(n==2) return 1;
  else if(n>2) return fib(n-1)+fib(n-2);
  else return -1;
  }
loop from 1 to the limit

display fib(i)

VERSION II

fibrec()
{
 a=0;      b=1;
 }
int fib(int n)
{
if(n>0)
{
c = a + b;      a = b;       b = c;
display c;

n--;

fib(n);
}
return b;
}



No comments: