Here are some simple code to display various diamond shape in C sharp console application. The diamond shape display challenges you in usage of for loop. First the very simple one display a half triangle shape like.
*
**
***
****
*****
for this we need to use nested for loop and the first loop take care of the vertical limits and the second loop take care of the horizontal lines.
eg:
for (int i = 0; i <= 5; i++)
{
for (int j=0; j = 0; i–)
{
for (int j = 0; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
this will output as follows
*****
****
***
**
*
Finally the complete diamond shape like as follows
*
**
****
******
********
**********
*************
***********
*********
*******
*****
***
*
the code is simple though
1) first we need to specify an empty string to track of the spaces
2) a for loop to track of the vertical axis
3) second for loop to add the space to the string
4) third for loop just add a * after that space to the string
by this four steps we get the first half of the diamond
by reversing this one back we will get the next part of the diamond
source:
String line = "";
for (int i = 0; i < 7; i++)
{
line = "";
for (int j = 0; j < 7 – i; j++)
{
line = line + " ";
}
for (int k = 0; k = 0; i–)
{
line = “”;
for (int j = 0; j < 7 – i; j++)
{
line = line + " ";
}
for (int k = 0; k < 1 + i * 2; k++)
{
line = line + "*";
}
Console.WriteLine(line);
}
And if you have any queries please comment me…Thank you for viewing this post.
Hi Kailash,
How r u? I hope you can remember me.
How is your studies?. Try to put a post for atleast monthly once.
sure buddy…
brother this code is not working for me .. i have to submit my assignment till friday please can u again check this code and send it to me pleaseeeeeeeeeeeeeeeeeee
What is the error or you are getting any format mistake…
yes i am.
oyeh we have same code in having triangle and diamond
display the following pattern in C# programming:
*
* *
* * *
* * * *
* * * * *
Hi can u write a full code for V shape by using x and the hight of V is 10 x plz send me rply as my assignment due on this friday plz
The code in C# plz