Let’s take an example of simple triangle,
First we have to find the relation between values of ‘i‘ and star (j),
i=1 -> j=1
i=2 -> j=3
i=3 -> j=5
i=4 -> j=7
After observing above conditions, we have the relation j=2*i-1.
Now for whitespaces…
We have the number of rows i.e. n=4, here in the example…
i=1 -> j=3
i=2 -> j=2
i=3 -> j=1
i=4 -> j=0
i.e. relation is j=n-i
So now, we have got the relation for both the star and whitespaces.
Now the last step is to put these relations in for loop in given below Algorithm.
1. Number of rows n=4.
2. Repeat steps 3 to 11 for i=1,2,3,…….,n.
4. Repeat steps 5 to 6, while j>0.
5. Print ‘_’ (whitespace).
8. Repeat steps 9 and 10 while j<0.
9. Print ‘*’.
11. Print ‘new line’.
Like this we can find more relations for different patterns by just changing the value of ‘j ‘and number of for loops, rest of the programs remains almost same…