DSA SheetLesson · no judge

DSA FUNDAMENTALSCONTROL FLOW

Triangle Star Patterns

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 25

Row r contains exactly r stars

For n = 4, the triangle has four output lines. The first line has one star, the second has two, the third has three, and the fourth has four.

TEXTThe required output for four rows.
*
**
***
****

The row number tells you the star count directly: row 1 has 1 star, row 2 has 2 stars, row 3 has 3 stars, and row 4 has 4 stars. The rule is row r -> r stars. The nested loop must use this changing row value to create the stair-step shape.

the n = 4 left-aligned star triangle arranged on a four-row, four-column guideA four-row, four-column guide has row labels 1 through 4 and column labels 1 through 4. Row 1 has a star only in column 1. Row 2 has stars in columns 1 and 2. Row 3 has stars in columns 1, 2, and 3. Row 4 has stars in all four columns. Cells where the column number is greater than the row number are empty, creating a stair-step right edge.**********12341234col  row
Each row stops printing when col reaches row.

The inner loop must stop at the current row, not at n

The outer loop visits rows 1 through 4. For each row, the inner loop starts col at 1 and prints stars while col is at most row. Because the loop condition uses row, its number of passes changes from one row to the next.

CPPThe inner loop uses col <= row, so each row gets its own star count.
int n = 4;

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        cout << "*";
    }
    cout << '\n';
}

When row = 3, the inner loop uses col values 1, 2, and 3, then stops. If its condition were col <= n, it would use col values 1, 2, 3, and 4 on every row. That would print four stars on all four rows, producing a square instead of a triangle.

CHECKPOINT 1Not answered

Which inner-loop condition prints exactly three stars when row = 3 in the n = 4 example?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The newline runs once per completed row

Star printing belongs inside the inner loop because each pass prints one star. Newline printing belongs immediately after the inner loop, because that is when the current row is complete. For row 3, three inner-loop passes print three stars, then one newline moves output to the next line.

CPPThe newline is outside the inner loop and inside the outer loop.
for (int row = 1; row <= 4; row++) {
    for (int col = 1; col <= row; col++) {
        cout << "*";
    }
    cout << '\n';
}

A newline inside the inner loop would move to a new line after every star, so the output would contain one star per line. A newline after the outer loop would wait until all ten stars had been printed, so the stars would appear on one line. Exactly one newline is needed after each completed row.

CHECKPOINT 2Not answered

Move or rewrite the newline statement so the output remains four lines with 1, 2, 3, and 4 stars.

for (int row = 1; row <= 4; row++) {
    for (int col = 1; col <= row; col++) {
        cout << "*";
        cout << '\n';
    }
}

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A four-row trace proves that the code prints ten stars

The inner-loop counts for rows 1 through 4 are 1, 2, 3, and 4. Adding them gives 1 + 2 + 3 + 4 = 10 star prints. The newline after each inner loop preserves the four row boundaries, so those ten prints appear as the required triangle rather than as one uninterrupted line.

ROWCOL VALUES USEDSTARS PRINTEDNEWLINE
111once after the inner loop
21, 22once after the inner loop
31, 2, 33once after the inner loop
41, 2, 3, 44once after the inner loop
Total10 star prints104 newlines
The inner-loop trace for n = 4.

The same row-star rule and newline placement work in both C++ and Java. In each implementation, row runs from 1 through 4, col runs only through row, and the newline runs once after the inner loop.

CPPC++ implementation of the n = 4 triangle.
int n = 4;

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        cout << "*";
    }
    cout << '\n';
}
JAVAJava implementation of the n = 4 triangle.
int n = 4;

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        System.out.print("*");
    }
    System.out.println();
}
Previous · Square Star and Rhombus Star PatternsNext part · Quiz