Triangle V2 - C# Programming Exercise

In this C# exercise, you are asked to create a program that asks the user for their name and then displays a triangle made from that name, starting with 1 letter and growing until it shows the full name.

The program should work as follows:

- First, ask the user for their name.
- Then, display the triangle, where the first line has just the first letter, the second line has the first two letters, and so on, until the last line displays the full name.

This exercise will help you practice string manipulation and the use of loops in C# to efficiently perform repetitive tasks.

 Category

Arrays, Structures and Strings

 Exercise

Triangle V2

 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

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

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

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#.

  •  Rectangle V3

    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.

  •  Centered triangle

    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...

  •  Cities database

    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...

  •  Banner

    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...

  •  Triangle right side

    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...

  •  Strings manipulation

    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...