I have demonstrated how to print Diamond shape of stars and Diamond Space in square using for loops.
Introduction
In continuation of my previous article I have demonstrated to print diamond of stars and diamond in square with explanation.
Objective
Improving Logical skills and printing various types of shapes using For loop
Using the code
Print Diamond of Stars
In this article let me explain how to print diamond of stars using C# programming.
In my last article we saw the triangle facing towards left and right side. But now we have to make traingle facing upwards and downwards for making diamond of stars.
For example purpose I hardcoded the value 11 as maximum.Instead of this we can use a variable and assign the value after getting input from user
For making triangle facing upside:
for (int i = 0; i < 11; i++) //Outer loop
{
for (int j=0;j<11-i;j++) //First Inner loop
{
Console.Write(" ");
}
for (int k = 1; k < i; k++) //Second loop
{
Console.Write("*");
}
for (int m = 0; m< i; m++) //Third loop
{
Console.Write("*");
}
Console.WriteLine();
}
The first for loop is used for how many rows to be printed for this triangle. Here I started from 0 to 11. So 10 rows will be printed.Inside this loop I used 3 for loops and I used
three variables on each loop namely j,k,m respectively.
To get diamond we need to print 1 star in first row. 3 stars in 2nd row. 5 stars in 3rd row. This logic will continue till the outer "i" finishes.
The following is the pictorial representation if we replace SPACE with s in the first inner "j" for loop.
0 1 2 3 4 5 6 7 8 9 10................19
0 S S S S S S S S S S S
1 S S S S S S S S S S *
2 S S S S S S S S S * * *
3 S S S S S S S S * * * * *
4 S S S S S S S * * * * * * *
5 S S S S S S * * * * * * * * *
6 S S S S S * * * * * * * * * * *
7 S S S S * * * * * * * * * * * * *
8 S S S * * * * * * * * * * * * * * *
9 S S * * * * * * * * * * * * * * * * *
10 S * * * * * * * * * * * * * * * * * * *
At the first iteration of "i" outer loop: when i==0
The first inner loop "j" checks upto j<11-0 so it prints 11 spaces since j starts from 0.---> Please refer the first row of above picture. The second and third loops will not be executed
since the condition fails when i==0.
For K loop 1<0 condition fails
For m loop 0<0 condition fails
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE
At the second iteration of "i" outer loop : When i==1
The first inner loop "j" checks upto j<11-1
so it prints 10 spaces since j starts from 0.---> Please refer the second row of above picture.
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE
In the second loop i.e k loop: k<i--> 1<1 --->condition fails. Similarly the condition fails upto 11<1 (2<1,3<1,4<1 and so on)
At the end of "k" loop we have
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE
Coming to the thrid loop m<i:
At first iteration of third loop : 0<1 ---> the condition gets true and 1 star will be printed.
From second iteration the condition gets fails 1<1,2<1 and so on.
Since there is no WriteLine() method (only Write() method) the star prints very next to 10th space.
The output is
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE *
At the third iteration of "i" outer loop : When i==2
The first inner loop "j" checks upto j<11-2 so it prints 9 spaces since j starts from 0.---> Please refer the third row of above picture.
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE
In the second loop i.e k loop:
k<i--> 1<2 --->condition true.--->1st time
k<i--->2<2 --->Condition fails---->2nd time, upto 11<2. so now we have at the end of K loop
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE *
Coming to the thrid loop m<i:
At first iteration of third loop : 0<2 condition true
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * *
At second iteration of "m" loop : 1<2 condition true
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * * *
At third iteration of "m" loop : 2<2 condition fails. So it comes out of third inner loop and prints new line.
At the fourth iteration of "i" outer loop : When i==3
The first inner loop "j" checks upto j<11-3 so it prints 8 spaces since j starts from 0.---> Please refer the third row of above picture.
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE
In the second loop i.e k loop: k<i--> 1<3 --->condition true.--->1st time
k<i--->2<3 --->Condition true --->Second time
k<i--->3<3 --->Condition fails-->3rd time, upto 11<3. so now we have at the end of K loop
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * *
Coming to the thrid loop m<i:
At first iteration of third loop : 0<3 condition true
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * * *
At second iteration of "m" loop : 1<3 condition true
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * * * *
At third iteration of "m" loop : 2<3 condition true
SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE SPACE * * * * *
From fourth iteration of "m" loop : 4<2 condition starts to fail. So it comes out of third inner loop and prints new line.
After iteration of all values of outer loop "i" we have
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
Similarly for printing triangle at downside:
for (int i = 11; i > 0; i--)
{
for (int j = 0; j < 11 - i; j++)
{
Console.Write(" ");
}
for (int k = 1; k < i; k++)
{
Console.Write("*");
}
for (int m = 0; m < i; m++)
{
Console.Write("*");
}
Console.WriteLine();
}
The following is the pictorial representation if we replace SPACE with s in the first inner "j" for loop.
0 1 2 3 4 5.... ....20
0 * * * * * * * * * * * * * * * * * * * * *
1 S * * * * * * * * * * * * * * * * * * *
2 S S * * * * * * * * * * * * * * * * *
3 S S S * * * * * * * * * * * * * * *
4 S S S S * * * * * * * * * * * * *
5 S S S S S * * * * * * * * * * *
6 S S S S S S * * * * * * * * *
7 S S S S S S S * * * * * * *
8 S S S S S S S S * * * * *
9 S S S S S S S S S * * *
10 S S S S S S S S S S *
Here I used the same logic as of before except I am making the for loop printing in reverse direction.
At the first iteration of "i" outer loop: when i==11
The first inner loop "j" checks j<11-11 -->0
so 0<0 --> so condition fails and come out of the first inner "j" loop.
For K loop 1<11 condition true prints a star
2<11 condition true prints a star
3<11 condition true prints a star
upto 10<11, then when 11<11 comes condition fails.
So now we have 10 stars
* * * * * * * * * *
For m loop 0<11 condition true prints a star
1<11 condition true prints a star
2<11 condition true prints a star
upto 10<11 then when 11<11 comes condition fails
this m loop runs for 11 times so we have 21 stars now and zero space
* * * * * * * * * * * * * * * * * * * * *
At the second iteration of "i" outer loop: when i==10
The first inner loop "j" checks j<11-10 -->1
so 0<1 --> so condition true and prints a SPACE
1<1 --> fails and come out of the first inner "j" loop.
Now we have
SPACE
For K loop 1<10 condition true prints a star
2<10 condition true prints a star
3<10 condition true prints a star
upto 9<10, then when 10<10 comes condition fails.
So now we have 9 stars
SPACE * * * * * * * * *
For m loop 0<10 condition true prints a star
1<10 condition true prints a star
2<10 condition true prints a star
upto 9<10 then when 10<10 comes condition fails
this m loop runs for 10 times so we have 19 stars now and one space
SPACE * * * * * * * * * * * * * * * * * * *
At the third iteration of "i" outer loop: when i==9
The first inner loop "j" checks j<11-9 -->2
so 0<2 --> so condition true and prints a SPACE
1<2 --> so condition true and prints a SPACE
2<1 --> fails and come out of the first inner "j" loop.
Now we have
SPACE SPACE
For K loop 1<9 condition true prints a star
2<9 condition true prints a star
3<9 condition true prints a star
upto 8<9, then when 9<9 comes condition fails.
So now we have 8 stars
SPACE SPACE * * * * * * * *
For m loop 0<9 condition true prints a star
1<9 condition true prints a star
2<9 condition true prints a star
upto 8<9 then when 9<9 comes condition fails
this m loop runs for 9 times so we have 17 stars now and two spaces
SPACE SPACE * * * * * * * * * * * * * * * * *
Similarly after iteration of all "i" values we have
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
If we write both code snippets one after another then we will get a diamond shape of collection of stars as follows:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
To Print diamond space inside a square like follows:
***********************************
***************** *****************
**************** ****************
*************** ***************
************** **************
************* *************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
************* *************
************** **************
*************** ***************
**************** ****************
***************** *****************
***********************************
Here I have used only two loops.I initialized the outer "i" loop to 35 so that we can get the output in visible format. I have declared an integer variable called kk and reinitializing to 0 on each iteration.
I am initializing "j" loop from 0 to 35 in ascending order.
Inside "j" loop we have If/else condition. Here only our main logic occurs.
First of all in "if" condition I am checking the outer loop "i" value is less then the half of the given number. Here it is 17. Inside the if conditon I have
another "if" condition which has two conditions with OR operator.
For the first Iteration of "i" : i==0
For the first iteration of "j" : j==0
if (0<=35/2) --> if (0<=17) Condition gets true, then in inner if condition
For the first iteration of "j" : j==0
if j <= (i - n / 2) or (j >= (n / 2) + i) --> here I am using 17 for n/2.
if (0 <= 17-0) or (0>= (17)+0) --> o<=17 or 0>=17
0<=17 -- condition true
0>17 -- condition fails
since one condition is true, then one star will be printed.
similarly for j value for 1 ---> 1<=17 or 1>=17 first condition gets true prints star
2<=17 or 2>=17 first condition gets true prints star
upto
17<=17 or 17>=17 first condition gets true prints star
for j value 18 18<=17 or 18>=17 first condition fails but second condition is true so prints star
for j value 19 19<=17 or 19>=17 first condition fails but second condition is true so prints star
upto last iteration i.e j is 34.
***********************************
for the first row.
For the second Iteration of "i" : i==1
For the first iteration of "j" : j==0
if (1<35/2) --> if (1<17) Condition gets true, then in inner if condition
For the first iteration of "j" : j==0
if (0 <= 17-1) or (0>= (17)+1) --> 0<=16 or 0>18
0<=17 -- condition true
0>17 -- condition fails
since one condition is true, then one star will be printed.
if we see this line 0<=16 or 0>=18 it is clear that both condition will fail when j value is 17. So now
similarly for j value for 1 ---> 1<=16 or 1>=18 first condition gets true prints star
2<=16 or 2>=18 first condition gets true prints star
upto
16<=16 or 16>=18 first condition gets true prints star
for j value 17 17<= 16 and 17 >= 18 ---> Here both condition fails. So compiler will print SPACE instead of star.
for j value 18 18<=16 or 18>=18 first condition fails but second condition is true so prints star
for j value 19 19<=16 or 19>=18 first condition fails but second condition is true so prints star
upto last iteration i.e j is 34.
0123.... 17 18..... 34
****************SPACE******************
for the second row.
For the third Iteration of "i" : i==2
For the first iteration of "j" : j==0
if (2<35/2) --> if (2<17) Condition gets true, then in inner if condition
For the first iteration of "j" : j==0
if (0 <= 17-2) or (0>= (17)+2) --> 0<=15 or 0>19
0<=15 -- condition true
0>19 -- condition fails
since one condition is true, then one star will be printed.
if we see this line 0<=15 or 0>=19 it is clear that both condition will fail when j value is 16,17,18. So now
similarly for j value for 1 ---> 1<=15 or 1>=19 first condition gets true prints star
2<=15 or 2>=19 first condition gets true prints star
upto
15<=15 or 15>=19 first condition gets true prints star
for j value 16 16<=15 or 16>=19 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 17 17<=15 or 17>=19 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 18 18<=15 or 18>=19 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 19 19<=15 or 19>=19 first condition fails but second condition is true so prints star
for j value 19 20<=15 or 20>=19 first condition fails but second condition is true so prints star
upto last iteration i.e j is 34.
0123.... 16 17 18 ..... 34
****************SPACESPACESPACE****************
For the fourth Iteration of "i" : i==3
For the first iteration of "j" : j==0
if (3<35/2) --> if (3<17) Condition gets true, then in inner if condition
For the first iteration of "j" : j==0
if (0 <= 17-3) or (0>= (17)+3) --> 0<=14 or 0>20
0<=14 -- condition true
0>20 -- condition fails
since one condition is true, then one star will be printed.
if we see this line 0<=14 or 0>=20 it is clear that both condition will fail when j value is 15,16,17,18,20 So now
similarly for j value for 1 ---> 1<=14 or 1>=20 first condition gets true prints star
2<=14 or 2>=20 first condition gets true prints star
upto
14<=14 or 14>=20 first condition gets true prints star
for j value 15 15<=14 or 15>=20 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 16 16<=14 or 16>=20 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 17 17<=14 or 17>=20 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 18 18<=14 or 18>=20 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 19 19<=14 or 19>=20 ----> Here both condition fails. So compiler will print SPACE instead of star.
for j value 20 20<=15 or 20>=20 first condition fails but second condition is true so prints star
for j value 21 21<=15 or 21>=20 first condition fails but second condition is true so prints star
upto last iteration i.e j is 34.
0123.... 15 16 17 18 19 ..... 34 for the fourth row.
***************SPACESPACESPACESPACESPACE***************
When i value becomes 18 the the if condition fails. (Outer "if" condition which is inside "j" For loop)
upto this we will be getting the following shape, i.e first part or upper part of the diamond.
*********************************** i= 0
***************** ***************** i= 1
**************** **************** i= 2
*************** *************** i= 3
************** ************** i= 4
************* ************* i= 5
************ ************ i= 6
*********** *********** i= 7
********** ********** i= 8
********* ********* i= 9
******** ******** i= 10
******* ******* i= 11
****** ****** i= 12
***** ***** i= 13
**** **** i= 14
*** *** i= 15
** ** i= 16
* * i= 17
when (i <= n / 2) condition fails, the "i" value would be 18. From this iteration onwarsd "else" part executes.
In "else" part also we have two conditions connecting with "OR" operator. The condition is
(j <= (i - n / 2)) || (j >= (n - kk))
Here I have used the variable "kk" for printing the stars on right side of the lower diamond. I am reinitializing the variable "kk" every time to zero on each iteration of outer "i" loop.
So that we can get the correct number of stars on the right hand side of the diamond.
In this example
we need to print on the 18th iteration of "i" loop like follows:
**SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS** (S-->denotes SPACE. In below examples I used S insted of SPACE to shortend the string)
On the nineteenth iteration of outer "i" loop:
On the first iteration of "j" loop ---
when j==0
(0<=18-17) OR (0>=(35-0))
Here first condition gets true, So compiler prints a star on the left hand side and increments the "kk" to 1.
Now we have *
when j==1 --> (1<=18-17) OR (1>=(35-1)) --> (1<=1) OR (1>=34) --->First condition gets true,prints Star
--->KK value will be incremented to 2.
Now we have **
when j==2 --> (2<=18-17) OR (2>=(35-2)) --> (2<=1) OR (2>=33) ---> Both condition fails, prints "SPACE" in else part. ---> KK value will not be incremented
Now we have **S
when j==3 --> (3<=18-17) OR (3>=(35-2)) --> (3<=1) OR (3>=33) ---> Both condition fails, prints "SPACE" in else part.
---> KK value will not be incremented
Now we have **SS
Similarly both condition will fail till j value becomes 33 and prints SPACE.
when j== 33 --> (33<=18-17) OR (33>=(35-2)) --> (33<=1) OR (33>=33)
---> Second condition gets true, (j >= (n - kk)) since kk value is 2 and and n value is 35 -->35-2=33.
Now star will be printed and kk value will be incremented to 3
Now we have **SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS*
when j== 34 --> (34<=18-17) OR (34>=(35-3)) --> (34<=1) OR (34>=32)
---> Second condition gets true, since kk value is 3 and and n value is 35 -->35-3=32.
Now star will be printed and kk value will be incremented to 4
0123...... 32 33 34
Now we have **SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS**
(Due to lack of space I just wrote the "j" value up with spaces.The number 34 denotes "j" value that prints last star)
Now j loop will be over and the control switch to next iteratio of "i" loop. Note that kk value will be reinitialized to zero.
On the twentieth iteration of outer "i" loop:
On the first iteration of "j" loop --- when j==0
(0<=19-17) OR (0>=(35-0)) ---> 0<=2 OR 0>=35.
Here first condition gets true, So compiler prints a star on the left hand side and increments the "kk" to 1.
Now we have *
when j==1 --> (1<=19-17) OR (1>=(35-1)) --> (1<=2) OR (1>=34) --->First condition gets true,prints Star
--->KK value will be incremented to 2.
Now we have **
when j==2 --> (2<=19-17) OR (2>=(35-2)) --> (2<=2) OR (2>=33) ---> First condition gets true,prints Star
---> KK value will be incremented to 3.
Now we have ***
when j==3 --> (3<=19-17) OR (3>=(35-3)) --> (3<=2) OR (3>=32) ---> Both condition fails, prints "SPACE" in else part.
---> KK value will not be incremented
Now we have ***S
Similarly both condition will fail till j value becomes 32 and prints SPACE.
when j== 32 --> (32<=19-17) OR (32>=(35-3)) --> (32<=2) OR (32>=32) ---> Second condition gets true, (j >= (n - kk)) since kk value is 3 and and n value is 35 -->35-3=32.
Now star will be printed and kk value will be incremented to 4
Now we have ***SSSSSSSSSSSSSSSSSSSSSSSSSSSSS*
when j== 33 --> (33<=19-17) OR (33>=(35-4)) --> (33<=2) OR (33>=31) ---> Second condition gets true, since kk value is 4 and and n value is 35 -->35-4=31.
Now star will be printed and kk value will be incremented to 5
Now we have ***SSSSSSSSSSSSSSSSSSSSSSSSSSSSS**
when j== 34 --> (34<=19-17) OR (34>=(35-5)) --> (34<=2) OR (34>=30) ---> Second condition gets true, since kk value is 5 and and n value is 35 -->35-5=30.
Now star will be printed and kk value will be incremented to 6
0123...... 32 33 34
Now we have ***SSSSSSSSSSSSSSSSSSSSSSSSSSSSS***
Now j loop will be completed and the control would be switched to next iteratio of "i" loop. Note that kk value will be reinitialized to zero.
On the twentyfirst iteration of outer "i" loop:
On the first iteration of "j" loop --- when j==0
(0<=20-17) OR (0>=(35-0)) ---> 0<=3 OR 0>=35.
Now we have *
Here first condition gets true, So compiler prints a star on the left hand side and increments the "kk" to 1.
when j==1 --> (1<=20-17) OR (1>=(35-1)) --> (1<=3) OR (1>=34) --->First condition gets true,prints Star
--->KK value will be incremented to 2.
Now we have **
when j==2 --> (2<=20-17) OR (2>=(35-2)) --> (2<=3) OR (2>=33) ---> First condition gets true,prints Star
---> KK value will be incremented to 3.
Now we have ***
when j==3 --> (3<=20-17) OR (3>=(35-3)) --> (3<=3) OR (3>=32) ---> First condition gets true,prints Star
---> KK value will be incremented to 4.
Now we have ****
when j==4 --> (4<=20-17) OR (4>=(35-4)) --> (4<=3) OR (4>=31) ---> Both condition fails, prints "SPACE" in else part.
---> KK value will not be incremented.
Now we have ****S
when j==5 ---> (5<=20-17) OR (5>=(35-4)) --> (5<=3) OR (5>=31) ---> Both condition fails, prints "SPACE" in else part.
---> KK value will not be incremented
Now we have ****SS
Similarly both condition will fail till j value becomes 31 and prints SPACE.
when j== 31 --> (31<=20-17) OR (31>=(35-4)) --> (31<=2) OR (31>=31) ---> Second condition gets true, (j >= (n - kk)) since kk value is 4 and and n value is 35 -->35-4=31.
Now star will be printed and kk value will be incremented to 5
Now we have ****SSSSSSSSSSSSSSSSSSSSSSSSSSS*
when j== 32 --> (32<=20-17) OR (32>=(35-5)) --> (32<=3) OR (32>=30) ---> Second condition gets true, since kk value is 5 and and n value is 35 -->35-5=30.
Now star will be printed and kk value will be incremented to 6
Now we have ****SSSSSSSSSSSSSSSSSSSSSSSSSSS**
when j== 33 --> (33<=20-17) OR (33>=(35-6)) --> (33<=3) OR (33>=29) ---> Second condition gets true, since kk value is 6 and and n value is 35 -->35-6=29.
Now star will be printed and kk value will be incremented to 7
Now we have ****SSSSSSSSSSSSSSSSSSSSSSSSSSS***
when j== 34 --> (34<=20-17) OR (34>=(35-7)) --> (34<=3) OR (34>=28) ---> Second condition gets true, since kk value is 7 and and n value is 35 -->35-7=28.
Now star will be printed and kk value will be incremented to 8
0123...... 31 33
Now we have ****SSSSSSSSSSSSSSSSSSSSSSSSSSS****
30 32 34
Now j loop will be completed and the control would be switched to next iteration of "i" loop. Now "kk" value will be zero.
Like this "i" will be incremeted and the process continuous till i value become 34.
So we will get when i is between 18 and 34. In the last row we will get all stars without space.
** ** i= 18
*** *** i= 19
**** **** i= 20
***** ***** i= 21
****** ****** i= 22
******* ******* i= 23
******** ******** i= 24
********* ********* i= 25
********** ********** i= 26
*********** *********** i= 27
************ ************ i= 28
************* ************* i= 29
************** ************** i= 30
*************** *************** i= 31
**************** **************** i= 32
***************** ***************** i= 33
*********************************** i= 34
For the all value of outer "i" ranging from 0 to 34 we get,
***********************************
***************** *****************
**************** ****************
*************** ***************
************** **************
************* *************
************ ************
*********** ***********
********** **********
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************ ************
************* *************
************** **************
*************** ***************
**************** ****************
***************** *****************
***********************************
Conclusion
I hope all of us have understood the logic behind printing of stars in different shapes. Any suggestions or feedback always welcome to improve my article quality.