Triangular star pattern programmes with examples
What are star Patterns?
Star pattern is a series of * character arranged in a manner to create some pattern or any required shape including different types of triangles, squares, rhombus, heart, and other shapes.
What are Triangular star Patterns?
Triangular star patterns are combinations of stars printed to form a shape which results in a triangle.
Some students think that why do they need to practice such programs. These programs are usually taught to students and are recommended by many developers to practice at the beginning of their programming career. these programs are helpful for the beginners in their logical building.
So dear developers, practice more patterns and get ready for more logical challenges. Its gonna help you a lot for sure.
Before going through codes, there a few things to keep in mind.
- $limiter is the limit to print stars. It can be any static number like 5, 8, etc. or can be a user input too.
- $breaker is used to make loops for printing line breaks.
- $star, $spacer, etc are used to print characters which are shown as their names are. (main pattern characters.)
- I am taking variable $limiter = 5.
- The larger the value of $limiter, the bigger will be the patterns.
Pattern #1: Half triangular pattern
This pattern is the most basic program to start with. To print this structure, I am using two for loops. First is to print line breaks and second(the inner one) is to print stars.
<?php
$limiter = 5;
for ($breaker = 0; $breaker < $limiter; $breaker++) {
for ($star = 0; $star <= $breaker; $star++) {
echo '*';
}
echo '<br>';
}
?>
Loop explanation:
- $breaker = 0
- $star = 0, i.e. equal to 0, prints 1 star.
- $star =1, i.e. greater than 0, condition false -> exit loop and print line break of outer loop.
- *
- $breaker = 1
- $star = 0, i.e. less than 1, prints a star.
- $star = 1, i.e. equal to 1, prints a star.
- $star =2, i.e. greater than 1, condition false -> exit loop and print line break of outer loop.
- * *
- $breaker = 2
- $star = 0, i.e. less than 2, prints a star.
- $star = 1, i.e. less than 2, prints a star.
- $star = 2, i.e. equal to 2, prints a star.
- $star = 3, i.e. greater than 2, condition false -> exit loop and print line break of outer loop.
- * * *
- $breaker = 3
- $star = 0, i.e. less than 3, prints a star.
- $star = 1, i.e. less than 3, prints a star.
- $star = 2, i.e. less than 3, prints a star.
- $star = 3, i.e. equal to 3, prints a star.
- $star = 4, i.e. greater than 3, condition false -> exit loop and print line break of outer loop.
- * * * *
- $breaker = 4
- $star = 0, i.e. less than 4, prints a star.
- $star = 1, i.e. less than 4, prints a star.
- $star = 2, i.e. less than 4, prints a star.
- $star = 3, i.e. less than 4, prints a star.
- $star = 4, i.e. equal to 4, prints a star.
- $star = 5, i.e. greater than 4, condition false -> exit loop and print line break of outer loop.
- * * * * *
- $breaker = 5, Equals to $limiter -> Breaks loop exit
The above code iterations result this pattern.
Pattern #2: Reverse Half triangle pattern
In this pattern, the outer loop is starting from higher value and goes to lower value unlike the previous one. And the inner loop is having the same functionality as in previous programs which results in the reverted triangular star pattern.
<?php
for ($breaker = $limiter; $breaker > 0; $breaker--) {
for ($star = $limiter; $star > ($limiter - $breaker); $star--) {
echo '*';
}
echo '<br>';
}
?>
Pattern #3: Mirror image of simple triangular pattern #1
Look wise this pattern is a mirror image of the Pattern #1. In this program, along with stars, spaces are also print to show the mirror image look. Here I used three loops. First as in previous one prints line breaks. Inner first loop prints spaces and second inner loop prints stars. The number of stars is increasing as the number of spaces decreasing.
<?php
for ($breaker = $limiter; $breaker > 0; $breaker--) {
for ($spacer = $limiter - 1; $spacer > ($limiter - $breaker); $spacer--) {
echo ' ';
}
for ($star = 0; $star < ($spacer + 1); $star++) {
echo '*';
}
echo '<br>';
}
?>
Pattern #4: Mirror image of revert triangular pattern #2
This program is a bit similar to Pattern #3. Like in Patter #3 along with stars, spaces are also print to show the mirror image look. In this one, spaces are increasing and stars are decreasing. Three loops are used. First loop prints line breaks. Inner first loop prints spaces and second inner loop prints stars.
<?php
for ($breaker = $limiter; $breaker > 0; $breaker--) {
for ($spacer = (0); $spacer <= ($limiter - $breaker - 1); $spacer++) {
echo ' ';
}
for ($star = $limiter; $star > ($limiter - $breaker); $star--) {
echo '*';
}
echo '<br>';
}
?>
Pattern #5: A combination of #1 and #2 to make this triangular star pattern
Here I am using a combination of two programs. The first half of this pattern is printed using the program for patter #1 and for the second half, I am using the program of pattern #2 but starting with a lesser row count i.e, 4 rows only. Here is the result.
<?php
for ($breaker = 0; $breaker < $limiter; $breaker++) {
for ($star = 0; $star <= $breaker; $star++) {
echo '*';
}
echo '<br>';
}
for ($breaker = $star - 1; $breaker > 0; $breaker--) {
for ($star2 = $limiter; $star2 > ($limiter - $breaker); $star2--) {
echo '*';
}
echo '<br>';
}
?>
Pattern #6: Mirror image of Patter #5
Last for this article but more on the way.
To make this pattern, I am using a combination of two programs again. The first half of this pattern is printed using the program for patter #3 and for the second half, I am using the program of pattern #4 but starting with a lesser row count i.e, 4 rows only.
<?php
for ($breaker = $limiter; $breaker > 0; $breaker--) {
for ($spacer = $limiter - 1; $spacer > ($limiter - $breaker); $spacer--) {
echo ' ';
}
for ($star = 0; $star < ($spacer + 1); $star++) {
echo '*';
}
echo '<br>';
}
for ($breaker = $limiter - 1; $breaker > 0; $breaker--) {
for ($spacer = (0); $spacer < ($limiter - $breaker); $spacer++) {
echo ' ';
}
for ($star = $limiter; $star > ($limiter - $breaker); $star--) {
echo '*';
}
echo '<br>';
}
?>
Using combinations like this, a lot of programs can be made and practiced.
Thanks, sir for providing this information. I am a web developer. This bog very uses full for me.