You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Recursive implementation to calculate the n-th Fibonacci numberintfibonacci(int n)
{
// Base case: n = 0 or 1if (n <= 1)
{
return n;
}
// Recursive case: fib(n) = fib(n - 1) + fib(n - 2)returnfibonacci(n - 1) + fibonacci(n - 2);
}
✅ Solution 02 - Iterative
intfibonacci(int n)
{
if (n <= 0)
return;
int a = 0, b = 1;
for (int i = 0; i < n; i++)
{
cout << a << "";
longlong nextTerm = a + b;
a = b;
b = nextTerm;
}
}