How to print Hollow Pyramid Pattern in Perl - Step by Step Examples
How to print Hollow Pyramid Pattern in Perl ?
Answer
To print a hollow pyramid pattern in Perl, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
✐ Examples
1 Hollow Pyramid Pattern (Height: 5) in Perl
In this example,
- Set the number of rows for the pyramid.
- Use nested loops: one for rows, one for spaces, and one for stars.
- In the inner loops, print spaces for the spaces between stars and print stars for the pyramid edges.
- Adjust conditions to print stars only at the pyramid edges and spaces elsewhere to create the hollow effect.
- Print each row to create the hollow pyramid pattern.
Perl Program
#!/usr/bin/perl
use strict;
use warnings;
sub print_hollow_pyramid {
my $rows = shift;
for my $i (1..$rows) {
my $pattern = ' ' x (($rows - $i) * 2);
for my $j (1..(2 * $i - 1)) {
if ($j == 1 || $j == (2 * $i - 1) || $i == $rows) {
$pattern .= '* ';
} else {
$pattern .= ' ';
}
}
print "$pattern\n";
}
}
print_hollow_pyramid(5);
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in Perl language with well detailed examples.
More Perl Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in Perl ?
- How to print Right Half Pyramid Pattern in Perl ?
- How to print Pyramid Pattern in Perl ?
- How to print Rhombus Pattern in Perl ?
- How to print Diamond Pattern in Perl ?
- How to print Hour Glass Pattern in Perl ?
- How to print Hollow Square Pattern in Perl ?
- How to print Hollow Pyramid Pattern in Perl ?
- How to print Hollow Inverted Pyramid Pattern in Perl ?
- How to print Hollow Diamond Pattern in Perl ?
- How to print Floyd's Trianlge Pattern in Perl ?
- How to print Pascal's Triangle Pattern in Perl ?