⚡ TL;DR
Build a right-angled star triangle in Dart with nested loops. Full code with main() function, expected output, and practice exercises for new Dart developers.
The right triangle star pattern is the “Hello World” of loop exercises in any language. Here’s how to build it in Dart, with nested loops and clean console output.
What We’ll Build
*
* *
* * *
* * * *
* * * * *Each row shows i stars in row i, starting from row 1.
Basic Approach: Nested For Loops
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
// Inner loop prints stars
for (int j = 1; j <= i; j++) {
print("*");
}
print(""); // Newline after each row
}
}Output
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*Wait — this is wrong! It’s printing one star per line. You need a horizontal space between stars.
Fixed Approach: String Buffer
Use StringBuffer (Dart’s efficient string builder) or the end parameter isn’t available in Dart’s print():
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
StringBuffer row = StringBuffer();
for (int j = 1; j <= i; j++) {
row.write("* ");
}
print(row.toString().trimRight());
}
}Output
*
* *
* * *
* * * *
* * * * *Approach 2: Using List.filled
For cleaner functional style:
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
print(List.filled(i, "*").join(" "));
}
}Output
*
* *
* * *
* * * *
* * * * *Approach 3: With Numbers Instead of Stars
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
List<String> row = [];
for (int j = 1; j <= i; j++) {
row.add(j.toString());
}
print(row.join(" "));
}
}Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Approach 4: Right-Aligned (Left Triangle)
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
String spaces = " " * (n - i);
String stars = List.filled(i, "*").join(" ");
print(spaces + stars);
}
}Output
*
* *
* * *
* * * *
* * * * *Dart-Specific Tips
StringBufferis more efficient thanString +=in loopsprint()always adds a newline — noendparameter like PythonList.filled(count, value)creates repeated elements quickly- Use
String * intfor repetition:"*" * 5→"*****"
Complexity Analysis
| Metric | Reason |
|---|---|
| Time | O(n²) — total iterations = n(n+1)/2 |
| Space | O(n) — each row is a temporary string |
Practice Variations
- Inverted triangle: start with
nstars, end with 1 - Number triangle:
1,2 2,3 3 3 - Character triangle:
A,B B,C C C - Hourglass: right triangle followed by inverted triangle
Running the Code
dart run main.dartOr paste into DartPad — Dart’s online editor.
Next pattern: Inverted Right Triangle Star in Dart
Related Patterns
FAQ
How do you print the Right Triangle Star pattern in Dart?
Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Dart implementation with expected output is shown in the sections above.
What is the time complexity of the Right Triangle Star pattern in Dart?
The time complexity is O(n²) and the space complexity is O(1), since the pattern is built with a fixed number of loop counters and printed row by row.
