Printing Pattern Using Loops hackerrank solution

      
                #include <stdio.h>

                int main() {
                    int n;
                    scanf("%d", &n);
                
                    int size = 2 * n - 1;
                    int start_row = 0, end_row = size - 1;
                    int start_col = 0, end_col = size - 1;
                
                    int matrix[size][size];
                
                    while (start_row <= end_row && start_col <= end_col) {
                        for (int i = start_col; i <= end_col; i++) {
                            matrix[start_row][i] = n;
                        }
                        start_row++;
                
                        for (int i = start_row; i <= end_row; i++) {
                            matrix[i][end_col] = n;
                        }
                        end_col--;
                
                        for (int i = end_col; i >= start_col; i--) {
                            matrix[end_row][i] = n;
                        }
                        end_row--;
                
                        for (int i = end_row; i >= start_row; i--) {
                            matrix[i][start_col] = n;
                        }
                        start_col++;
                
                        n--;
                    }
                
                    for (int i = 0; i < size; i++) {
                        for (int j = 0; j < size; j++) {
                            printf("%d ", matrix[i][j]);
                        }
                        printf("\n");
                    }
                
                    return 0;
                }
                
            

Let's go through the solution step by step:

We include the header file to use the standard input/output functions.

In the main() function, we declare an integer n to store the input value.

We use the scanf() function to read input from the user and store it in the variable n.

We calculate the size of the matrix required to print the pattern. It is determined by 2 * n - 1.

We declare variables start_row, end_row, start_col, and end_col to keep track of the boundaries of the pattern.

We declare a 2D integer array matrix with dimensions size by size to store the pattern.

We use a while loop to iterate until the boundaries are properly set.

Inside the loop, we use four nested for loops to populate the elements of the matrix according to the pattern.

The first loop fills the top row of the pattern with the current value of n.

The second loop fills the right column of the pattern with the current value of n.

The third loop fills the bottom row of the pattern with the current value of n.

The fourth loop fills the left column of the pattern with the current value of n.

After each loop, we update the boundary values and decrement n to move to the next layer of the pattern.

Once the pattern is constructed, we use two nested for loops to print the elements of the matrix.

Finally, we use the printf() function to print each element of the matrix followed by a space. After printing each row, we add a newline character to move to the next line.

By providing the expected input and executing the code, you should see the desired output according to the problem statement on HackerRank.