⚡ TL;DR
Print the pyramid star pattern in Dart. Complete working code, expected output, algorithm explanation, and practice variations for beginners.
The Pyramid Star pattern is a fundamental loop exercise for Dart beginners. Here’s the complete implementation with working code and output.
Pattern to Print
*
***
*****
*******
*********Implementation
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
print(" " * (n - i) + "*" * (2 * i - 1));
}
}Output
*
***
*****
*******
*********How It Works
Each row i has (n-i) leading spaces and (2i-1) stars — the odd-number sequence that centers the triangle.
Practice Variations
- Try printing the pattern with a different character (e.g.,
#or+) - Modify the code to accept the pattern size as user input
- Combine this pattern with its inverse to create a diamond or hourglass
Complexity
- Time: O(n²) — total characters printed grows quadratically
- Space: O(n) — longest row is O(n) characters
Related Patterns
FAQ
How do you print the Pyramid 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 Pyramid Star pattern in Dart?
The time complexity is O(n²) and the space complexity is O(n), since the pattern is built with a fixed number of loop counters and printed row by row.
Happy coding!
