Why do I get the error that the series starts from 11?

0
#include <iostream>

using namespace std;
int fib(int n);

int main()

{
    int numero,i;
    cout<<"ingrse el numero de elementos"<<endl;
    for(i=1;i<=numero;i++){
        cout<<i<<fib(i)<<endl;
    }


return 0;
}

int fib(int n){
    if(n==0 || n==1)
        return n;

else{
return fib(n-1) + fib(n-2);
}
return 0;
}
    
asked by JOSE DANIEL MIRANDA CALDERON 03.09.2018 в 03:32
source

1 answer

4

There is no error that the series starts at 11, what happens is that you are not putting any separator between the element counter and the cout<<i<<fib(i)<<endl; additional element, in that code you did not assign the variable number cin >> numero; .

I put a separator "-", and fixing some little details of spacing and tabulations, it should look like this.

#include <iostream>

using namespace std;
int fib(int n);

int main() {
    int numero,i;
    cout << "ingrese el numero de elementos: " ;
    cin >> numero;
    for(i=1; i<=numero; i++){
        cout << i << " - " << fib(i) << endl;
    }
    return 0;
}

int fib(int n){
    if(n==0 || n==1) {
        return n;
    } else {
    return fib(n-1) + fib(n-2);
    }
    return 0;
}

The exit:

ingrese el numero de elementos: 7
1 - 1
2 - 1
3 - 2
4 - 3
5 - 5
6 - 8
7 - 13
    
answered by 03.09.2018 в 05:09