The pattern has 4 rows, and each row contains stars (*) in a decreasing number:
Row 0: 4 characters (****) 0 Space
Row 1: 3 Digit(***) 1 Space
Row 2: 2 characters(**) 2 Spaces
Row 3: 1 Digit(*) 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 characters in each row).
3) In each iteration of the outer loop:
--> The inner loop first prints spaces, and in second inner loop prints the respective character for the row.
-->The number of spaces in each row is equal to the row index (row).
-->The number of characters in each row is total_rows - row.
-->The characters alternate between letters (starting with 'A'), and digits (starting with '1').
Java |
public class Main { public static void main(String[] args) { int total_rows = 4; // Number of rows char current_char = 'A'; // Starting character // 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 characters for (int col = 0; col < total_rows - row; col++) { if (row % 2 == 0) { System.out.print(current_char); // Print alphabetic characters on even rows } else { System.out.print(row+1); // Print numbers on odd rows } } if (row % 2 == 0) current_char++; // Increment to the next letter for even rows System.out.println(); // Move to the next line } } } |
C++ |
#include <iostream> using namespace std; int main() { int total_rows = 4; // Number of rows char current_char = 'A'; // Starting character // Outer loop for rows for (int row = 0; row < total_rows; row++) { // Inner loop for spaces for (int space = 0; space < row; space++) { cout << " "; } // Inner loop for characters for (int col = 0; col < total_rows - row; col++) { if (row % 2 == 0) { cout << current_char; // Print alphabetic characters on even rows } else { cout << (row +1); // Print numbers on odd rows } } if (row % 2 == 0) current_char++; // Increment to the next letter for even rows cout << endl; // Move to the next line } return 0; } |
Python |
total_rows = 4 # Number of rows current_char = 'A' # Starting character # Outer loop for rows for row in range(total_rows): # Inner loop for spaces for space in range(row): print(" ", end="") # Inner loop for characters for col in range(total_rows - row): if row % 2 == 0: print(current_char, end="") # Print alphabetic characters on even rows else: print(row+1, end="") # Print numbers on odd rows if row % 2 == 0: current_char = chr(ord(current_char) + 1) # Increment to the next letter for even rows print() # Move to the next line |