Printing Pattern Using Loops

  • + 0 comments

    include

    include

    include

    include

    int main() { int n,i; scanf("%d", &n); int size = 2*n-1; int a[size][size];

    int top = 0;
    int bottom=size-1;
    int left=0;
    int right=size-1;
    
    while(top<=bottom && left<=right)
    {
        for(i=left;i<=right;i++)
        {
            a[top][i]=n;
        }
        top++;
        for(i=top;i<=bottom;i++)
        {
            a[i][right]=n;
        }
        right--;
        for(i=right;i>=left;i--)
        {
            a[bottom][i]=n;
        }
        bottom--;
        for(i=bottom;i>=top;i--)
        {
            a[i][left]=n;
        }
        left++;
    
        n--;
    }
    
    
    for(i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    

    }