0

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. :(

Edited by superdhebz: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by WaltP
0

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

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.