Hi. I have two homeworks which is to make a Fibonacci series using iteration and recursion. Im finished with iteration. I got the right code for recursion its just that, I don't know how to show the numbers for the fibonacci, it only shows the answer.

Here's the code:

#include <iostream>
#include <conio.h>

using namespace std;

int fib(int num);

int main()
{
    int num, ans;    
    int n1 = 1;
    int n2 = 1;
    
    cout << "Enter a number/position to find: ";
    cin >> num;
    
    cout << "\n\nProcessing...\n\n";
    cout << n1 << " " << n2 << " ";
    
    ans = fib(num);
    cout << endl << endl;
    
    cout << ans << " is the " << num << "th Fibonacci number.";
    system("PAUSE");
    return 0;
}

int fib(int num)
{
    if (num < 3)
    {
       return (1);
    }
    else
    {   
        return( fib (num-2) + fib (num-1) );
    }
}

If you enter 6. You will be able to get 8 as the 6th position in the Fibonacci series. But you should be seeing the numbers 1 1 2 3 5 8 there. :(

Simple.
Split this line return( fib (num-2) + fib (num-1) ); into
1) compute the value
2) print the value
3) return the value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.