Solution
The pattern has 4 rows, and each row contains stars (*) in a decreasing number:
Row 0: 4 stars (****) 0 Space
Row 1: 3 stars (***) 1 Space
Row 2: 2 stars (**) 2 Spaces
Row 3: 1 star (*) 3 Spaces
To print this pattern, we need two loops:
1) Outer loop for the rows.
2) Inner loop for the columns (printing spaces and stars in each row).
3)In each iteration of the outer loop:
-->The inner loop first prints spaces, and then second inner loop prints stars.
-->The number of spaces printed in each row is equal to the row index (row).
-->The number of stars printed in each row is equal to total_rows - row.
Java |
public class Main { public static void main(String[] args) { int total_rows = 4; // Number of rows // Outer loop for rows for (int row = 0; row < total_rows; row++) { // Inner loop for spaces for (int space = 0; space < row; space++) { System.out.print(" "); } // Inner loop for stars for (int col = 0; col < total_rows - row; col++) { System.out.print("*"); } System.out.println(); // Move to the next line } } } |
C++ |
#include <iostream> int main() { // Outer loop for rows return 0; |
Python |
total_rows = 4 # Number of rows # Outer loop for rows for row in range(total_rows): # Inner loop for spaces for space in range(row): print(" ", end="") # Inner loop for stars for col in range(total_rows - row): print("*", end="") print() # Move to the next line |