711658. Pattern 6

Basic

Here is the Markdown blog post for the GeeksforGeeks problem "Pattern 6":

711658: Pattern 6

Summary

This problem involves generating a triangular pattern based on a given input. The key concept involved is array manipulation, specifically building and printing triangles.

Detailed Explanation

The problem requires us to generate a triangular pattern of asterisks (*) where the number of rows (n) is provided as input. Each row should have one more asterisk than the previous row. For example, if n = 5, the output would be:

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

To solve this problem, we can use a simple loop that iterates over each row of the triangle and prints the corresponding number of asterisks.

Here's the step-by-step breakdown:

  1. Initialize an integer variable i to keep track of the current row.
  2. Start a loop that runs from 1 to n (inclusive).
  3. Inside the loop, print i asterisks followed by a newline character (\n) to start a new line.
  4. Increment i for each iteration of the loop.

Time complexity: O(n), where n is the input number of rows. Space complexity: O(1), as we only use a constant amount of space to store the current row index and other variables.

Optimized Solutions

Java

View on GeeksforGeeks
public class Pattern6 {
    public static void main(String[] args) {
        int n = 5; // input number of rows
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.