How To Triangular star pattern programmes with examples

Triangular star pattern programmes with examples

Easy Star pattern programs in PHP

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.

  1. $limiter is the limit to print stars. It can be any static number like 5, 8, etc. or can be a user input too.
  2. $breaker is used to make loops for printing line breaks.
  3. $star, $spacer, etc are used to print characters which are shown as their names are. (main pattern characters.)
  4. I am taking variable $limiter = 5.
  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.

Loop explanation:

  1. $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.
    • *
  2. $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.
    • * *
  3. $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.
    • * * *
  4. $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.
    • * * * *
  5. $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.
    • * * * * *
  6. $breaker = 5, Equals to $limiter -> Breaks loop exit

The above code iterations result this pattern.

Star pattern simple - 1

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.

Star pattern simple - 2

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.

Star pattern simple - 3

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.

Half Triangle pattern simple - 4
Star pattern simple – 4

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.

Triangle pattern simple - 5
Star pattern simple – 5

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.

Triangle pattern simple - 6
Star pattern simple – 6

Using combinations like this, a lot of programs can be made and practiced.

Leave a Reply