To print an Hourglass Pattern in Dart, we use nested loops. The outer loop controls the rows, and the inner loops control the spaces and stars (*) to form the pattern.
In this example,
printHourglass
that takes the desired number of rows n
as a parameter.for
loops, where the outer loop runs from n
to 1 and the inner loops control the spaces and stars in each row.void printHourglass(int n) {
for (int i = n; i >= 1; i--) {
print(' ' * (n - i) + '*' * (2 * i - 1));
}
for (int i = 2; i <= n; i++) {
print(' ' * (n - i) + '*' * (2 * i - 1));
}
}
void main() {
printHourglass(5);
}
********* ******* ***** *** * *** ***** ******* *********
In this tutorial, we learned How to print Hour Glass Pattern in Dart language with well detailed examples.