Calculate fabonaci(Recursive& Iterative way)

View previous topic View next topic Go down

Calculate fabonaci(Recursive& Iterative way)

Post  Admin on Fri Sep 09, 2011 10:04 pm

Iterative way:

public int fabonaci(int n){
if(n<=0)
return 0;
int previous=-1;
int current=1;
for(int i=0;i<=n;i++){
int sum=previous+current;
previous=current;
current=sum;
}
return sum;
}

Recursive way:

public int fabonaci(int n){
if(n<=0)
return 0;
if(n==1)
return 1;
return fabonaci(n-1)+fabonaci(n-2);
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum