Here's the code:
class qual
{
public static int fibonacci(int n)
{
if (n == 0 || n == 1)
{
return 1;
}
else
{
return fibonacci(n-1) + fibonacci(n-2);
}
}
public static void main(String[] arg)
{
System.out.println(fibonacci(5));
}
}
The output was 8.
The output should be 8 but when I look at this I think it should be 7 ((5-1) +(5-2)
).
Why was the output 8? I think the reasoning behind getting 8 will make recursion maybe stop being confusing for me.