Objective
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
Write Your C# Exercise
C# Exercise Example
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
}
}
}
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from that name.
In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from t...
In this exercise, you are asked to create a database to store information about cities. In the first approach, you will store only the name...
This Exercise in C# involves creating a program that imitates the basic Unix SysV "banner" utility, allowing you to display large texts in a similar manner. The purpo...
This Exercise in C# involves creating a program that asks the user for a text string and displays a right-aligned triangle using that string. The triang...
This Exercise in C# involves creating a program that asks the user for a text string and performs three specific transformations on it. The program must...