If you look at Rosetta Code page for Fibonacci, you see that F(0) == 0
and F(1) == 1
.
int fibonacci(int n)
{
if (n == 0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1) + fibonacci(n-2);
}
return fib;
}
In this case, you have a function that will calculate the fibonacci number at a specific position, right?
So now you need to calculate them and then print them:
int main()
{
int n;
cin >> n;
if (n < 0)
{
return -1; // This means there was an error
}
for (int i = 1; i < n; ++i)
{
cout << fibonacci(i) << " ";
}
return 0;
}
Note that this is not the most efficient way to do it at all, but it kinda helps you understand how recursion works.