To print a Hollow Diamond Pattern in Dart, we use nested loops to manage spaces and stars.
In this example,
// Print Hollow Diamond Pattern in Dart
void main() {
int numRows = 5;
for (int row = 1; row <= numRows; row++) {
String pattern = '';
for (int space = 1; space <= numRows - row; space++) {
pattern += ' ';
}
for (int star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
pattern += '*';
} else {
pattern += ' ';
}
}
print(pattern);
}
for (int row = numRows - 1; row >= 1; row--) {
String pattern = '';
for (int space = 1; space <= numRows - row; space++) {
pattern += ' ';
}
for (int star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
pattern += '*';
} else {
pattern += ' ';
}
}
print(pattern);
}
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Diamond Pattern in Dart language with well detailed examples.