Printing Fibonacci series

// This is what I need to do
Take N (number of rows), print the following pattern (for N = 4)
0
1 1
2 3 5
8 13 21 34

//This is my code
#include
using namespace std;

int main() {
    int N;
    cin>>N;
    int numberO = 0;
    int numberZ = 1;
    int numberX;
    cout<<numberO<<endl<<numberZ<<" "<<numberZ<<endl;
    for(int i = 2 ; i <=N-1; ++i){
        for(int j = 0 ; j <=i ; ++j){
            numberX = numberO+numberZ;
            cout<<numberO+numberZ<<" ";
            numberO = numberZ;
            numberZ = numberX;
        }
        cout<<endl;
    }
    return 0;
    }

can anyone help me withwhats wrong ?
just cant figure out why the test case wont pass

1 Like

Mention Problem link .

On N=4
Expected Output
0
1 1
2 3 5
8 13 21 34
Your output
0
1 1
1 2 3
5 8 13 21

Hit like if u get it :stuck_out_tongue:

1 Like

You are making mistake inside the for loop. It should be like this:-
int numberO = 0;
int numberZ = 1;
int numberX=1;
cout<<numberO<<endl<<numberZ<<" “<<numberZ<<endl;
for(int i = 2 ; i <=N-1; ++i){
for(int j = 0 ; j <=i ; ++j){
numberX = numberX+numberZ;
cout<<numberX<<” ";
numberO = numberZ;
numberZ = numberX;
numberX=numberO;
}
cout<<endl;
}

next number is sum of previous two, keeping on updating the previous continuously.
Hit Like if you get it! :stuck_out_tongue: