DEV Community

Discussion on: Learning Algorithms with JS, Python and Java 9: Printing Steps

Collapse
 
jastbytes profile image
Jan Steffen • Edited

This is what I came up with in C#. Sure there are many possible soultions. :)

Iterative Solution

static void Steps(int n)
{
    var step = new char[n];
    Array.Fill(step, ' ');

    for (var i = 0; i < n; i++)
    {
        step[i] = '#';
        Console.WriteLine(step);
    }
}

Recursive Solution

static void Steps(int n, int i = 0, char[] step = null)
{
    if (i == n) return;
    if (step == null)
    {
        step = new char[n];
        Array.Fill(step, ' ');
    }

    step[i] = '#';
    Console.WriteLine(step);

    Steps(n, ++i, step);
}
Collapse
 
tommy3 profile image
tommy-3

Thank you for your comment. I've never learned any C-family language and I thought they were much different from the languages I know, but the code actually looks quite familiar to me.

Collapse
 
jastbytes profile image
Jan Steffen

I think C# is more similar to modern languages than C++. The syntax of C++ gives me creep's every time. ;-)