Nested Loops in C for Pattern Printing

To create a nested loop to output a shape, it is often necessary to use nested looping statements to control the rows and columns of the output. Here is an example program that uses nested loops to output a simple rectangle shape:

#include <stdio.h>

int main() {
    int rows = 5;
    int cols = 10;

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}

In the example above, the outer loop controls the number of rows, the inner loop controls the number of columns, printing a “*” each time in the inner loop and a newline character “\n” at the end of each row. You can adjust the values of rows and cols to output different sizes of shapes according to your needs.

bannerAds