## knewc

``````#include <iostream>
using namespace std;

int fib(int n);

int main()
{

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

cout << "\n\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

## Ancient Dragon 5,243

why don't you put the call to fib() in a loop and pass the loop counter in the parameter

## knewc

do you have an example?

## Ancient Dragon 5,243

do you know how to write a loop?

## vmanes 1,165

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.

## knewc

``````#include <iostream>
using namespace std;

int fib(int n);

int main()
{

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

system("PAUSE");
return 0;
}

int fib(int n){
int counter = 0;
if (n < 2 ){
cout << "Fibonacci(" << n << ") = 1" << endl;
}

for(int n; n < 20; n++){
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

## vmanes 1,165

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.

## knewc

``````#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){
if (n < 2 ){
cout << "Fibonacci(" << n << ") = 1" << endl;
}
else{
n = fib(n-1)+fib(n-2);
}
}``````

still not working lol, i'm a noob

## Ancient Dragon 5,243

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.

## BlackJavaBean 3

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.