Printing Pattern Using Loops

  • + 0 comments

    There were definitely better ways to accomplish this...

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main()
    {
        int n;
        scanf("%d", &n);
        // Complete the code to print the pattern.
        int line_length = n*2-1;
        int line_counter = 0;
        int reverse_point = line_length / 2;
        int reverse_flag = 0;
        int iteration_counter = 0;
    
        while (iteration_counter != line_length){
            for (int j = 0; j < line_counter; j++) {
                printf("%d ", n-j);
            }
    
            for (int k = 0; k < line_length - line_counter * 2; k++) {
                printf("%d ", n-line_counter);
            }
    
            for (int l = 0; l < line_counter; l++) {
                printf("%d ", n-line_counter+l+1);
            }
    
            if (reverse_flag == 1) {
                line_counter--;
            } else {
                line_counter++;
            }
    
            if (line_counter >= reverse_point) {
                reverse_flag++;
            }
    
            iteration_counter++;
            printf("\n");
        }
        return 0;
    }