Hello Developers, Do you really want to learn How to write the Star Pattern programs in PHP?. of course yes, Because, It is very helpful to write any type of complex program very quickly. Also, It is asked in most of the interview round.
In this tutorial, I will share with you my best trick to create easily PHP start pattern programs with some simple steps that will be very easy to understand. Once, you learn it, you can easily create logic and write a standard program for any type of start pattern.
Steps to Write Pattern Program in PHP
Before getting started its coding, You must have a basic understanding of for loop and if-else condition. Because of, We will use both to create a complete program of start pattern.
Now, Follow all the next steps carefully otherwise, you may miss important concepts for writing a star pattern program.
First of all, I will learn to write a start pattern program for a right-angle triangle. After that, I will share more start pattern programs for other patterns in the upcoming articles.
I hope that, once you complete this article, you will definitely write programs for other patterns.
1. Draw Lines on each row & Column
In this step, you will have to follow the following steps –
Step-1: First of all, draw lines on each row & column of the given pattern
Step-2: Mark columns of pattern from left to right with counting numbers ( start from 1, 2, 3, & so on ) at the top side.
Step-3: Similarly, Mark rows of patterns from top to bottom with counting numbers ( start from 1, 2, 3, & so on ) on the left side.
Step-4: Assume i for columns of the pattern.
Step-5: Similarly, Assume j for the rows of the pattern.
i | ||||||
j | 1 | 2 | 3 | 4 | 5 | |
---|---|---|---|---|---|---|
1 | * | |||||
2 | * | * | ||||
3 | * | * | * | |||
4 | * | * | * | * | ||
5 | * | * | * | * | * |
2. Apply Outer & Inner For Loop
In this step, You have to follow the following steps –
Step-1: Declare the Outer For loop
First of all, Apply the outer for loop for i columns of the pattern. As columns are marked from 1 to 5. So, This loop will be declared with an initial value will be i=1, and the condition of the ends will be $i<=5 & increment value will be i++.
Finally, It will be declared as the following line of code –
for($i=1; $i<=5; $i++){ }
Step-1: Declare the Inner For loop
After that, Apply the inner for loop for i rows of the pattern. As columns are marked from 1 to 5. So, This loop will be declared with an initial value will be j= 1, and the condition of the ends will be $j<=5 & the increment value will be $j++.
Finally, It will be declared as the following line of code –
for($j=1; $j<=5; $j++){ }
Step-3: Declare the Outer & Inner For Loop Together.
// Outerfor loop for($i=1; $i<=5; $i++){ // inner for loop for($j=1; $j<=5; $j++){ } }
2. Write Position of Stars
In this step, you have to follow the following steps –
Step-1: First of all, Write i & j and write their value based on the position of the pattern.
Step-2: At row no. 1, a single star is placed in column no. 1. So, write the value of i is 1 and value of j is 1
Step-3: At row no. 2, two stars are placed in columns no. 1 & 2. So, write the value of i is 2, and values of j are 1 & 2.
Step-3: At row no. 3, three stars are placed in columns no. 1, 2 & 3. So, write the value of i is 3, and values of j are 1, 2 & 3.
Step-3: At row no. 4, four stars are placed in columns no. 1, 2, 3, & 4. So, write the value of i is 4, and values of j are 1, 2, 3, & 4.
Step-3: At row no. 5, five stars are placed in columns no. 1, 2, 3, 4 & 5. So, write the value of i is 5, and values of j are 1, 2, 3, 4 & 5.
i | j |
---|---|
1 | 1 |
2 | 1, 2 |
3 | 1, 2, 3 |
4 | 1, 2, 3, 4 |
5 | 1, 2, 3, 4, 5 |
3. Compare Position of Stars
In this step, you have to follow the following steps –
Step-1: At row no. 1, a single star is placed at column no 1. So, you can write the value of j is j<=1
Step-2: At row no. 2, two stars are placed at column no 2 or less. So, you can write the value of j is j<=2
Step-3: At row no. 3, three stars are placed at column no 3 or less. So, you can write the value of j is j<=3
Step-4: At row no. 4, four stars are placed at column no 4 or less. So, you can write the value of j is j<=4
Step-5: At row no. 5, two stars are placed at column no 5 or less. So, you can write the value of j is j<=5
i | j |
---|---|
1 | j<=1 |
2 | j<= 2 |
3 | j<= 3 |
4 | j<= 4 |
5 | j<= 5 |
4. Generalize the Position of stars
As you can see in the previous step, j is repeated in each row and its value is increased by 1 from 1 up to 5. So, We can generalize the value of j like the following lie code.
j<=i |
5. Print Stars based on IF Statement
As we have seen j<=i in the previous step, We have understood that, if the j<=i is true then stars will be printed
if($j<=$i){ echo "*"; }
5. Write Complete Pattern Program
As we have created a condition to print star pattern in the previous step. Now, Place it within the inner for loop and complete the program of star pattern.
// Outerfor loop for($i=1; $i<=5; $i++){ // inner for loop for($j=1; $j<=5; $j++){ if($j<=$i){ echo "*"; } } echo "<br>"; }