if (!(cin >> rows) || rows < 1) {
cout << "Enter a positive whole number.\n";
return 1;
}
Example 3 — Forward Loop + rows - i + 1
Ascending i with a decreasing star count — same shape without counting the outer loop backward.
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
int stars = rows - i + 1;
cout << string(stars, '*') << "\n";
}
return 0;
}
Output
*****
****
***
**
*
How It Works
1. Ascending outer loop.i still runs from 1 to rows — same direction as Program 1.
2. Shrinking star count.stars = rows - i + 1 so when i = 1 you get 5 stars, when i = 5 you get 1.
3. One-line row.string(stars, '*') builds the whole row; "\n" ends it.
Learn the countdown version first (Examples 1–2); use this when an interviewer asks for an ascending outer loop.
Edge Cases & Pitfalls
Check these before calling the solution done.
i++
Upright triangle by mistake
If you increment i from 1 to rows without changing the star bound, you reprint Program 1. Use i-- or print rows - i + 1 stars.
Newline inside
Column of stars
If cout << "\n" is inside the inner loop, each star lands on its own line. Use cout << "*" for stars; end the row only after the inner loop.
No newline
One endless line
Omitting the row break glues every star onto a single line.
rows = 1
Single star
Output is just * on one line — a good sanity check.
rows ≤ 0
Empty output
Outer loop never runs. Validate and re-prompt for interactive programs.
Bad input
Check cin.fail()
Letters leave cin in a failed state — prefer if (!(cin >> rows) || rows < 1).
Analysis
Time and Space Complexity
Program
Time
Extra space
Countdown nested loops (Examples 1–2)
O(rows²)
O(1)
Forward + string (Example 3)
O(rows²)
O(rows) temporary per row string
Total stars printed = n + (n - 1) + … + 1 = n(n+1)/2 — same as Program 1, still quadratic in n.
Remember
Key Takeaways
Rule: countdown i; row i prints exactly i stars.
Flip of Program 1: same inner loop — only reverse the outer loop.
Break the row: call cout << "\n" only after the inner loop.
Complexity:O(n²) time from the triangular star count; O(1) extra space for nested loops.
One line: for i from rows down to 1, print i stars with cout, then cout << "\n".
Frequently Asked Questions
The outer loop runs i from rows down to 1. For each i, the inner loop prints i stars. The first line uses i equal to rows so it is the longest; each later line has a smaller i, so the triangle points downward.
Program 1 uses for (i = 1; i <= rows; i++) so stars grow. This program uses for (i = rows; i >= 1; i--) so stars shrink. The inner loop still runs j from 1 to i.
Yes. Use for (i = 1; i <= rows; i++) and print (rows - i + 1) stars in the inner loop. Both styles produce the same shape.
cout << "*" stays on the same line. cout << "\n" ends the current line. Stars use cout << "*"; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows. Total stars are still n(n+1)/2, same as the upright triangle.
Yes. cout << string(i, '*') << "\n" prints a full row in one call while i counts down.
After cin >> rows, check cin.fail() or use if (!(cin >> rows)) to handle bad input.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
This inverted triangle uses the same inner loop as Program 1 — only the outer loop direction changes. Total stars stay n(n+1)/2, so complexity is still O(n²).