public class ModFib
{
public static int modFibonacci(int term)
{
if(term == 1)
{
return 3;
}
else if(term == 2)
{
return 5;
}
else
{
return modFibonacci(term - 1) + modFibonacci(term - 2) + modFibonacci(term - 3);
}
}
}
it works fine at only term - 3 but this gives a stack overflow error.
term = 3, you recursivly end up withmodFibonacci(3-3), which will end in an endless recursion, as your break condition wont ever be met again, as term wont ever get to be1or2again and you´ll endlessly end up the in theelsepart recursivly calling your method.