How to Display 4 Triangle Patterns Side by Side
c#
Solution
You didn't specify how you wanted to implement, so I present you with:
private static void Main()
{
Console.WriteLine(
@"* ******************** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
*********** ***********");
}
[EDIT] Ok, here's a less facetious answer. ;)
int n = 10;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j <= i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < 2*i; ++j)
Console.Write(" ");
for (int j = 0; j < n-i; ++j)
Console.Write("*");
for (int j = 0; j < n-i-1; ++j)
Console.Write(" ");
for (int j = 0; j <= i; ++j)
Console.Write("*");
Console.WriteLine();
}
[Second edit]
It would be more readable to write methods for outputting `n` stars or spaces, like so:
static void stars(int count)
{
for (int i = 0; i < count; ++i)
Console.Write("*");
}
static void spaces(int count)
{
for (int i = 0; i < count; ++i)
Console.Write(" ");
}
And then:
int n = 10;
for (int i = 0; i < n; ++i)
{
stars(i+1);
spaces(n-i-1);
stars(n-i+1);
spaces(2*i);
stars(n-i);
spaces(n-i-1);
stars(i+1);
Console.WriteLine();
}
Problem
I'm having trouble getting 4 different triangle patterns to appear side by side. This is a console application program. This is exactly what I'm trying to achieve through the use of nested for loops: ``` * ******************** * ** ********* ********* ** *** ******** ******** *** **** ******* ******* **** ***** ****** ****** ***** ****** ***** ***** ****** ******* **** **** ******* ******** *** *** ******** ********* ** ** ********* *********** *********** ``` I already have the individual patterns working already, but of course they appear one after the other. ``` using System; class Assignment5 { static void Main() { for (int i = 1; i <= 10; i++) // Outer loop for number of rows { for (int j = 1; j <= i; j++) // Inner loop for number of stars { Console.Write("*"); } Console.WriteLine(); } // End First Pattern for (int i = 10; i >= 1; i--) // Outer loop for number of rows { for (int j = 1; j <= i; j++) // Inner loop for number of stars { Console.Write("*"); } Console.WriteLine(); } // End Second Pattern for (int i = 10; i >= 1; i--) // Outer Loop for number of rows { for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces { Console.Write(" "); } for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars { Console.Write("*"); } Console.WriteLine(); } // End Third Pattern for (int i = 1; i <= 10; i++) //Outer Loop for number of rows { for (int j = 1; j <= 10 - i; j++) //Inner loop for number of spaces { Console.Write(" "); } for (int k = 1; k <= i; k++) //Secondary inner loop for number of stars { Console.Write("*"); } Console.WriteLine(); } // End Fourth Pattern Console.WriteLine("Press Enter for Part 2 of this Program"); Console.ReadKey(); Console.Clear(); } // End main function } // End class Assignment5 ```