how to print triangle pattern in c

#include <stdio.h>

int main() {
    int rows, i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

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

    return 0;
}

Explanation:

  1. The #include <stdio.h> line includes the standard input/output library, which provides functions like printf and scanf.

  2. The int main() function is the entry point of the program. It returns an int value, which is typically 0 to indicate successful execution.

  3. We declare three variables: rows, i, and j. rows is used to store the number of rows the user wants to print, while i and j are loop counters.

  4. The printf function is used to display a message asking the user to enter the number of rows.

  5. The scanf function is used to read the user's input and store it in the rows variable.

  6. The outer for loop is used to iterate over the rows. It starts from 1 and goes up to rows.

  7. The inner for loop is used to print the asterisks in each row. It starts from 1 and goes up to the current value of i.

  8. Inside the inner loop, the printf function is used to print an asterisk followed by a space.

  9. After printing all the asterisks in a row, the printf function is used to print a newline character (\n) to move to the next line.

  10. Once all the rows are printed, the return 0; statement is used to indicate successful program execution.

This program uses nested loops to print a triangle pattern of asterisks. The outer loop controls the number of rows, while the inner loop controls the number of asterisks in each row.