Exercise
Triangle V2
Objetive
Write a C# program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length:
Enter your name: Juan
J
Ju
Jua
Juan
Example Code
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 for their name
Console.Write("Enter your name: ");
string name = Console.ReadLine(); // Read the user's input as a string
// Loop to display the triangle, growing the string one character at a time
for (int i = 1; i <= name.Length; i++)
{
Console.WriteLine(name.Substring(0, i)); // Display the substring from the start to the i-th character
}
}
}