Hello eveyone! In this post I'm going to show you how to create diamond loop.
At the end of this post we will be able to see this output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
But before we can achieve our output we need to split in to two parts: Pyramid and the Inverted Pyramid.
Pyramid:
*
* *
* * *
* * * *
Inverted Pyramid:
* * * * *
* * * *
* * *
* *
*
Now that we have split the diamond into two parts, we can now set the Pyramid as our first task and the Inverted Pyramid as our second task.
Here's what will do on the first task. First, is to create a loop that will create a new line. Second, to create a loop that will help in forming the pyramid and lastly a loop that will create the body of the pyramid.
Code below for the first step:
For the second step:
For the last step:
We now then compile the codes.
And we are done with the first Pyramid. We will now proceed to the reverse pyramid or the second part of the pyramid. The process of creating the reverse pyramid is almost the same with the pyramid the only difference is that we switch the looping statement we created in the last step with the second step and changing the value in strResult.
This is how it looks like:
That's it! All we need to do now is to display on a TextBox with a Multiline enabled so that we can see the output.
Download SourceCode: Diamond Loop in CSharp and VB .Net
At the end of this post we will be able to see this output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
But before we can achieve our output we need to split in to two parts: Pyramid and the Inverted Pyramid.
Pyramid:
*
* *
* * *
* * * *
Inverted Pyramid:
* * * * *
* * * *
* * *
* *
*
Now that we have split the diamond into two parts, we can now set the Pyramid as our first task and the Inverted Pyramid as our second task.
Here's what will do on the first task. First, is to create a loop that will create a new line. Second, to create a loop that will help in forming the pyramid and lastly a loop that will create the body of the pyramid.
Code below for the first step:
for (i = 0; i <= 5; i++)
{
}
For the second step:
for (int j = i; j <= 5; j++)
{
strResult += " ";
}
For the last step:
for (int k = 1; k <= i; k++)
{
strResult += " *";
}
We now then compile the codes.
// The loop created on the first step
for (i = 0; i <= 5; i++)
{
// The loop created on the second step
// which will create the spaces that gives form
// to the pyramid
for (int j = i; j <= 5; j++)
{
strResult += " ";
}
// this will be the loop that will create the body
// of the pyramid
for (int k = 1; k <= i; k++)
{
strResult += " *";
}
// this will create a new line
// to move to the next line of the pyramid
strResult += System.Environment.NewLine;
}
And we are done with the first Pyramid. We will now proceed to the reverse pyramid or the second part of the pyramid. The process of creating the reverse pyramid is almost the same with the pyramid the only difference is that we switch the looping statement we created in the last step with the second step and changing the value in strResult.
This is how it looks like:
for (l = 0; l <= 5; l++)
{
for (int k =1; k <= l; k++)
{
strResult += " ";
}
for (int j = l; j <= 5; j++)
{
strResult += " *";
}
strResult += System.Environment.NewLine;
}
That's it! All we need to do now is to display on a TextBox with a Multiline enabled so that we can see the output.
Download SourceCode: Diamond Loop in CSharp and VB .Net