here is my cod:
public class Fibonacci {
public static int Fibo(int n){
//Base Case
if(n==1){ //Smaller Problem
return n;
}
int left=Fibo(n-1);
int right=Fibo(n-2);
int sum=left+right;
return sum;
}
public static void main(String Rgs[]){
System.out.print(Fibo(5));
}
}
I am getting error stack overflow i checked my base cas
Hi @shivabansfore123,
You have to put a condition for n=0 as well because when your n=2 then n-1 is equal to 1 it will return n to left which is one but at the same time below that right = fib(n-2) will also ask for a return value and n-2 = 0 so you have not specified anything so it will keep on running with n as negative value …
Please add a condition that if(n==0) return n; also
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.