To print a hollow pyramid pattern in Dart, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
void printHollowPyramid(int rows) {
for (int i = 1; i <= rows; i++) {
String pattern = ' ' * (rows - i) * 2;
for (int j = 1; j <= 2 * i - 1; j++) {
if (j == 1 || j == 2 * i - 1 || i == rows) {
pattern += '* ';
} else {
pattern += ' ';
}
}
print(pattern);
}
}
void main() {
printHollowPyramid(5);
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Dart language with well detailed examples.