To print a pyramid pattern in Perl, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
print_pyramid
that takes an integer n
as input to represent the number of rows in the pyramid.print
to move to the next line.sub print_pyramid {
my ($n) = @_;
for my $i (0..$n-1) {
print ' ' x ($n - $i - 1);
print '*' x (2 * $i + 1);
print "\n";
}
}
print_pyramid(5);
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in Perl language with well detailed examples.