Centered triangle Learn programming C#

Lesson:

Arrays, Structures and Strings


Exercise:

Centered triangle


Objetive:

Display a centered triangle from a string entered by the user:

__a__
_uan_
Juan


Code:

using System;
public class exercise85
{
    public static void Main()
    {
        string name;

        Console.Write("Enter your name: ");
        name = Console.ReadLine();

        if (name.Length % 2 == 0)
            name += " ";

        int position = name.Length / 2;
        int maxRows = name.Length / 2 + 1;
        int amount = 1;

        for (int i = 0; i < maxRows; i++)
        {
            for (int j = 0; j < position; j++)
                Console.Write(" ");
            Console.WriteLine(name.Substring(position, amount));
            position--;
            amount += 2;
        }
    }
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.