Printing Pattern Using Loops

  • + 0 comments
    #include <stdio.h>
    
    int main() 
    {
        int n;
        scanf("%d", &n);
    
        int size = 2 * n - 1;
    
        // Print the pattern
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                // Determine the value to print
                int min_distance = i < size - i - 1 ? i : size - i - 1;
                min_distance = j < size - j - 1 ? min_distance < j ? min_distance : j : min_distance < size - j - 1 ? min_distance : size - j - 1;
                printf("%d ", n - min_distance);
            }
            printf("\n");
        }
    
    }