Skip to content

Conditionals and Loops Printing Pattern Using Loops #23

@viharika-23

Description

@viharika-23

int main()
{
int n;
scanf("%d", &n);

// The size of the grid is (2*n - 1)
int len = 2 * n - 1;

// Iterate through rows
for(int i = 0; i < len; i++) {
    // Iterate through columns
    for(int j = 0; j < len; j++) {
        
        // Calculate distance to all four edges
        int min = i;
        if (j < min) min = j;
        if (len - 1 - i < min) min = len - 1 - i;
        if (len - 1 - j < min) min = len - 1 - j;
        
        // Print the value based on the minimum distance
        printf("%d ", n - min);
    }
    // Newline after each row
    printf("\n");
}

return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions