Objective
Write a C# program that prompts for a symbol and a width, and displays a triangle of that width, using that number for the inner symbol, as in this example:
Enter a symbol: 4
Enter the desired width: 5
44444
4444
444
44
4
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace to use basic classes like Console
class Program // Define the main class of the program
{
static void Main() // The entry point of the program
{
// Ask the user to enter a symbol
Console.Write("Enter a symbol: ");
char symbol = Console.ReadKey().KeyChar; // Read the symbol input from the user
Console.WriteLine(); // Move to the next line after the symbol input
// Ask the user to enter the desired width for the triangle
Console.Write("Enter the desired width: ");
int width = int.Parse(Console.ReadLine()); // Read the width and convert it to an integer
// Loop to display the triangle
for (int i = width; i > 0; i--) // Start from the width and decrease until 1
{
// Inner loop to print the symbol 'i' times in each row
for (int j = 0; j < i; j++) // Repeat the symbol 'i' times
{
Console.Write(symbol); // Print the symbol
}
Console.WriteLine(); // Move to the next line after printing the row
}
}
}