⚡ TL;DR
Print the letter diamond pattern in Dart. Complete working code, expected output, algorithm explanation, and practice variations for beginners learning loops.
The Letter Diamond pattern is a classic loop exercise. Here’s the complete implementation in Dart with working code and output.
Pattern to Print
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
AImplementation
void main() {
int n = 5;
for (int i = 1; i <= n; i++) {
String letters = '';
for (int j = 1; j <= i; j++) { letters += String.fromCharCode(64 + j); }
print(' ' * (n - i) + letters + letters.split('').reversed.skip(1).join());
}
for (int i = n - 1; i >= 1; i--) {
String letters = '';
for (int j = 1; j <= i; j++) { letters += String.fromCharCode(64 + j); }
print(' ' * (n - i) + letters + letters.split('').reversed.skip(1).join());
}
}Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
AHow It Works
The alphabet pyramid mirrored vertically: top half grows, bottom half shrinks back to A.
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 more complex shape
Complexity
- Time: O(n²) — the grid or line count grows quadratically
- Space: O(1) — only loop counters are needed
Related Patterns
- Letter Diamond in Python
- Letter Diamond in Go
- Letter Diamond in Swift
- Alphabet Hollow Diamond in Dart
- Repeated Letter Rows in Dart
- Alphabet Rectangle in Dart
FAQ
How do you print the Letter Diamond 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 Letter Diamond 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.
Happy coding!
