Ejercicio
Triángulo lado derecho
Objetivo
Cree un programa en C# que solicite al usuario una cadena y muestre un triángulo alineado a la derecha:
____n
___an
__uan
Juan
Código de Ejemplo
using System; // Importing the System namespace to use its functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
Console.Write("Enter your name: "); // Prompt the user to input their name
string name = Console.ReadLine(); // Read the input from the user
// Call the method to display the right-aligned triangle with the name
DisplayRightAlignedTriangle(name);
}
// Method to display the right-aligned triangle
static void DisplayRightAlignedTriangle(string text)
{
int length = text.Length; // Get the length of the input string
// Loop to print each line of the triangle
for (int i = 1; i <= length; i++) // i represents the number of characters to print in each row
{
// Print the spaces for the right alignment
for (int j = 0; j < length - i; j++)
{
Console.Write(" "); // Print spaces
}
// Print the substring of the name up to the current character (i)
Console.WriteLine(text.Substring(0, i)); // Display the substring of the text for the current row
}
}
}