Programming Course in C# ¡Free!

Centered triangle

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise


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

__c__
_ach_
Nacho


Output



Solution


using System;

public class StringTriangle
{

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;
}
}
}