lundi 12 juin 2023

Why is j not printing in j==n and why j gets printed as 6 when j==2?

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n-2;i++){
        for(int j=1;j<=n;j++){
            if(i==1||i==n-2){
                cout<<j;
            }
            else{
                if(j==1||j==n){
                    cout<<j;
                }
            }
        }
        cout<<endl;
    }
    return 0;
    }

input:

6

output I get:

123456
16
16
123456

Why am I getting 6 printed in j==2 when i is not 1 or n-2 instead of j==n.

I have expected a hollow rectangle.

123456
1    6
1    6
123456

Aucun commentaire:

Enregistrer un commentaire