Printing Pattern Using Loops

  • + 0 comments

    I have a solution which is way faster and more simple than using the array

    include

    include

    include

    include

    int main() {

    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    for (int i = 1; i <= 2 * n - 1; i++) {
        int d = abs(i - n);
        for(int j = 1; j <= n - 1 - d; j++) printf("%d ", n - j + 1);
        for(int j = 1; j <= 2 * d + 1; j++) printf("%d ", d + 1);
        for(int j = n - 1 - d; j >= 1; j--) printf("%d ", n - j + 1);
        printf("\n");
    
    }
    return 0;
    

    }