#include <iostream>
using namespace std;

                int fib(int n);

    int main()
   {

     int n, answer;
      cout << "Enter number to find: ";
     cin >> n;

    cout << "\n\n";

    answer = fib(n);

     cout << answer << " is the " << n << "th Fibonacci number\n";
         system("PAUSE");
         return 0;
}

  int fib (int n)
   {
    cout << "Processing fib(" << n << ")... ";

     if (n < 3 )
    {
       cout << "Return 1!\n";
       return (1);
     }
      else
     {
      cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";
      return( fib(n-2) + fib(n-1));
     }
 }

can i get this to print
fibonacci(1) = 1
fibonacci(2) = 1
fibonacci(3) = 6

all the way until i get the integer i entered

for example, if i would input 5, it would say
fibonacci(1) = 1
fibonacci(2) = 1
fibonacci(3) = 6
fibonacci(4) = 24
fibonacci(5) = 120

You could put a loop in main( ) that prints every fibonacci number up to the input value from the user.

Note that the results you show in your sample are not the Fibonacci series 1 1 2 3 5 8 13.... but your code does give correct results.

#include <iostream>
using namespace std;

                int fib(int n);

    int main()
   {

     int n, answer;
     cout << "Enter number for fibonacci sequence: ";
     cin >> n;

    answer = fib(n);
         system("PAUSE");
         return 0;
}

  int fib(int n){
      int answer;
      int counter = 0;
  if (n < 2 ){
       cout << "Fibonacci(" << n << ") = 1" << endl;
     }
    
     for(int n; n < 20; n++){
     answer = fib(n-1)+fib(n-2);
     counter++;
     cout << "Fibonacci(" << counter << ") = " << answer << endl;
     }
  }

Ok...this is what i have so far...how do i get it to work? lol...it runs when the < 20 in the for loop is changed to a > 2, however, this makes it run for a long time lol, so i want it to stop at the number i entered

The loop belongs in main( ) - there it calls your original fibonacci( ) function for each value of the loop counter, displaying the result at each loop iteration.

#include <iostream>
using namespace std;

                int fib(int n);

    int main()
   {

     int n, answer, counter = 0;
     cout << "Enter number for fibonacci sequence: ";
     cin >> n;
         system("PAUSE");
         return 0;
     for(int n; n < n; n++){        
     counter++;
     cout << "Fibonacci(" << counter << ") = " << fib(n) << endl;
     }
}

  int fib(int n){
      int answer;
  if (n < 2 ){
       cout << "Fibonacci(" << n << ") = 1" << endl;
     }
     else{
          n = fib(n-1)+fib(n-2);
  }
}

still not working lol, i'm a noob

line 14: use a different loop counter because that is destroying the value of n that you type in on line 11. Example: for(int i = 1; i <= n; i++){ line 16: replace n in the call to fib with the loop counter.

Don't forget to move your system("PAUSE") and return 0; statements down. Where they are now, they'll terminate before your program can do its job.

This article has been dead for over six months. Start a new discussion instead.