#include <stdio.h>

int main() {
    int rows;

    // Prompt user for the number of rows
    printf("Enter the number of rows for the inverted pyramid: ");
    scanf("%d", &rows);

    // Outer loop for each row
    for (int i = 0; i < rows; i++) {
        // Inner loop for printing spaces
        for (int j = 0; j < i; j++) {
            printf(" "); // Print space
        }
        // Inner loop for printing stars
        for (int k = 0; k < (2 * (rows - i) - 1); k++) {
            printf("*"); // Print star
        }
        // Move to the next line after each row
        printf("\n");
    }

    return 0;
}

Explanation of the Code

Header Files: The program includes stdio.h to use standard input and output functions.

Getting User Input:

We declare an integer variable rows to store the number of rows for the inverted pyramid.

We prompt the user to enter the desired number of rows.

Outer Loop:

The outer loop (for (int i = 0; i < rows; i++)) runs once for each row of the inverted pyramid.

Inner Loop for Spaces:

The first inner loop (for (int j = 0; j < i; j++)) prints spaces to create the indentation. The number of spaces increases as we move down the rows.

Inner Loop for Stars:

The second inner loop (for (int k = 0; k < (2 * (rows - i) - 1); k++)) prints the stars. The number of stars decreases as we go down each row, calculated by 2×(rows−i)−12 \times (\text{rows} – i) – 12×(rows−i)−1.

New Line:

After printing the stars for each row, we use printf("\n") to move to the next line.

Output:

Enter the number of rows: 5

*********
 *******
  *****
   ***
    *

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights