⚡ TL;DR
Print Floyd's Triangle in Dart using nested loops and a running counter. Full working code with output, plus variations like reverse Floyd's and right alignment.
Floyd’s Triangle is a right-angled triangle of consecutive natural numbers. The first row has 1 number, the second has 2, and so on — but the numbers keep incrementing across rows instead of resetting. It’s a classic loop exercise for mastering state that persists across iterations.
Pattern to Print
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15Implementation
void main() {
int n = 5;
int counter = 1;
for (int i = 1; i <= n; i++) {
List<String> row = [];
for (int j = 0; j < i; j++) {
row.add(counter.toString());
counter++;
}
print(row.join(" "));
}
}Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15How It Works
A single counter increments with every printed number, and row i contains exactly i numbers. The key insight: the counter lives outside the inner loop, so it persists across rows. That’s what makes Floyd’s Triangle different from patterns where each row resets to 1 2 3....
Variation: Right-Aligned Floyd’s
Pad each number to a fixed column width so the triangle’s right edge lines up:
void main() {
int n = 5;
int counter = 1;
int maxNum = n * (n + 1) ~/ 2; // last number printed
int width = maxNum.toString().length; // column width for alignment
for (int i = 1; i <= n; i++) {
List<String> row = [];
for (int j = 0; j < i; j++) {
row.add(counter.toString().padLeft(width));
counter++;
}
print(row.join(" "));
}
}Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15n * (n + 1) ~/ 2 is the closed-form for the last number (sum of 1..n), used to compute the padding width.
Variation: Reverse Floyd’s Triangle
Count downward, starting each row from where the previous row would have ended:
void main() {
int n = 5;
int total = n * (n + 1) ~/ 2;
for (int i = 1; i <= n; i++) {
List<String> row = [];
int start = total - i + 1;
for (int j = 0; j < i; j++) {
row.add((start + j).toString());
}
print(row.join(" "));
total -= i;
}
}Output
15
13 14
10 11 12
6 7 8 9
1 2 3 4 5Practice Variations
- Fibonacci Floyd’s: fill the rows with Fibonacci numbers instead of consecutive integers
- Even-only Floyd’s: print 2, 4, 6, 8… in triangle form
- Pyramid Floyd’s: center the numbers like a pyramid instead of left-aligning
- Modify the code to accept the triangle size as user input
Each variation exercises the same skill: tracking state across loop iterations. Master this and the harder pattern problems become straightforward.
Complexity
- Time: O(n²) — prints n(n+1)/2 numbers
- Space: O(n) — one row buffer at a time
Related Patterns
- Floyd’s Triangle in Python
- Floyd’s Triangle in Go
- Floyd’s Triangle in Swift
- Pascal’s Triangle in Dart
- Palindromic Number Pyramid in Dart
- Number Pyramid in Dart
FAQ
How do you print the Floyd’s Triangle 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 Floyd’s Triangle 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!
