Printing Pattern Using Loops

  • + 0 comments
    #include<stdio.h>
    
    int main(){
        int n;
        scanf("%d",&n);
        int l=((n-1)*2)+1;
        int matrix[l][l];
        int x=1,i,y=2;
        int mid=n-1;
    
        for(x;x<n;x++){
            i=0;
            for(i=0;i<l;i++){
                matrix[mid+x][i]=y;
                matrix[mid-x][i]=y;
            }
            i=0;
            for(i=0;i<l;i++){
                matrix[i][mid+x]=y;
                matrix[i][mid-x]=y;
            }
            y++;
        }
        matrix [mid][mid]=1;
        int j=0;
        i=0;
    
        for (i = 0; i < l; i++) {
            for ( j = 0; j < l; j++) {
                printf("%d ", matrix[i][j]);
            }
            printf("\n");
            j=0;
        }
        return 0;
    }