One can generate a Fibonacci Series using the following classes
-
Method 1:
public static int Fib(int nth){
int a=1;
int b=1;
int c=0;
int loop=3;
while(nth>3){
if (loop>nth){
break;
}
c=a+b;
a=b;
b=c;
loop++;
}
return nth<3?1:c;
}
Method 2: Using Recursion
public static int Fib(int nth){
if(nth<2){return 1;}
else{
return Fib(nth-1)+Fib(nth-2);
}
}
No comments:
Post a Comment