Printing Pattern Using Loops

  • + 0 comments

    include

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

    // Fill the matrix with the concentric layer numbers
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            // Calculate the minimum distance to any edge
            int p = i < size - i - 1 ? i : size - i - 1;
            p = p < (j < size - j - 1 ? j : size - j - 1) ? p : (j < size - j - 1 ? j : size - j - 1);
            out[i][j] = n - p;
        }
    }
    
    // Print the matrix with space-separated values
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d ", out[i][j]);
        }
        printf("\n");
    }
    
    return 0;
    

    }