Ejercicio
Triángulo centrado
Objetivo
Mostrar un triángulo centrado a partir de una cadena introducida por el usuario:
__a__
_uan_
Juan
Código de Ejemplo
using System; // Import the System namespace for basic functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Ask the user to input their name
Console.Write("Enter your name: ");
string name = Console.ReadLine(); // Read the user's input as a string
// Loop through the string and print each progressively longer substring, centered
for (int i = 1; i <= name.Length; i++)
{
// Create the substring from the first character to the current position
string substring = name.Substring(0, i);
// Calculate the number of spaces to center the substring
int spaces = (name.Length - i) / 2;
// Print the substring with the calculated number of spaces for centering
Console.WriteLine(new string(' ', spaces) + substring + new string(' ', spaces));
}
}
}