c# How to place triangles in a row horizontally?

Viewed 132
 Console.WriteLine("Enter the number of triangles: ");
        int n = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n + 1; j++)
            {
                for (int k = 0; k < j; k++)
                    Console.Write("+");
                Console.WriteLine();
            }
        }

The program outputs triangles in a row vertically. How to make the program display them horizontally in a row?

enter image description here

3 Answers

To write it horizontally, you'll need to store the values somewhere as you populate each line up to n. One option is storing it in an array.

For each triangle, you need to first fill it with + symbols, and then fill the remaining with .

So for input 5, you would have 5 triangles, each one going from 1 triangle on the first line, 2 triangles on the second line, all the way up to 5 triangles on the fifth line. Once you're done with looping, you should end up with something like this in your array for the first triangle.

var example = new string[] {
 "+    "
,"++   "
,"+++  "
,"++++ "
,"+++++"
};

For the second triangle, you would add a space and then extend the existing strings so they become longer.

var example = new string[] {
 "+     +    "
,"++    ++   "
,"+++   +++  "
,"++++  ++++ "
,"+++++ +++++"
};

And so on and so on up the n-th triangle.

Here's the example code.

Console.WriteLine("Enter the number of triangles: ");
int n = int.Parse(Console.ReadLine());
var lines = new string[n + 1];

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n + 1; j++)
    {
        // populate from 0 to j     with "+" characters
        for (int k = 0; k < j; k++)
            lines[j] += "+";
        // populate from j to n + 1 with " " characters
        for (int k = j; k < n + 1; k++)
            lines[j] += " ";
    }
}

// skip the first row
for (int i = 1; i < n + 1; i++)
    Console.WriteLine(lines[i]);

Here's what the output looks like:

Enter the number of triangles: 
5
+     +     +     +     +     
++    ++    ++    ++    ++    
+++   +++   +++   +++   +++   
++++  ++++  ++++  ++++  ++++  
+++++ +++++ +++++ +++++ +++++ 

Here's a LINQ version:

int n = int.Parse(Console.ReadLine());
var output =
    String.Join(
        Environment.NewLine,
        Enumerable.Range(0, n).Select(w =>
            String.Concat(
                Enumerable.Range(0, n).Select(x =>
                    new String('+', w + 1) + new String(' ', n - w))).Trim()));

With n == 3 I get this:

+   +   +
++  ++  ++
+++ +++ +++

With n == 7 I get this:

+       +       +       +       +       +       +
++      ++      ++      ++      ++      ++      ++
+++     +++     +++     +++     +++     +++     +++
++++    ++++    ++++    ++++    ++++    ++++    ++++
+++++   +++++   +++++   +++++   +++++   +++++   +++++
++++++  ++++++  ++++++  ++++++  ++++++  ++++++  ++++++
+++++++ +++++++ +++++++ +++++++ +++++++ +++++++ +++++++

So here it is my answer, hopefully you find it useful.

static void Main(string[] args)
{
    Console.WriteLine("Enter the number of triangles: ");
    var n = int.Parse(Console.ReadLine()!);

    for (var i = 0; i < n; i++)
    {
        var line = ConstructLine(n - i, i + 1, n);

        Console.Write(line);
        Console.WriteLine();
    }
}

private static StringBuilder ConstructLine(int numberOfSpacesInARow, int numberOfStarsInARow, int totalTrees)
{
    var sb = new StringBuilder();

    for (var i = 0; i < totalTrees; i++)
    {
        WriteNStars(numberOfStarsInARow, ref sb);
        WriteNSpaces(numberOfSpacesInARow, ref sb);
    }

    return sb;
}

private static void WriteNSpaces(int numberOfSpaces, ref StringBuilder sb)
{
    for (var i = 0; i < numberOfSpaces; i++)
    {
        sb.Append(' ');
    }
}

private static void WriteNStars(int numberOfStars, ref StringBuilder sb)
{
    for (var i = 0; i < numberOfStars; i++)
    {
        sb.Append('*');
    }
}
Related