Triangle - C# Programming Exercise

This exercise in C# aims to develop a program that prompts the user for a symbol and a width, and then displays a decreasing triangle of the specified width, using the given symbol to form the lines of the triangle. The program will progressively reduce the number of symbols in each line to create a triangle pattern.

This exercise is excellent for reinforcing concepts of control structures like loops in C#, specifically for loops, which are used to iterate and control the output of the pattern. It is a practical activity for learning about string manipulation and structured output in the console, essential skills in programming with C#. This type of exercise also helps develop problem-solving skills involving pattern generation.

 Category

Basic Data Types

 Exercise

Triangle

 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

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

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

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

  •  Password as string

    This exercise in C# aims to develop a program that asks the user for their username and password (both as strings) and repeats the prompt as many...

  •  Password 5 attempts

    This C# exercise involves creating a program that prompts the user for their username and password, both as strings. If the entered credentials do not m...

  •  Calculator - if

    This C# programming exercise requires creating a program that asks the user for two numbers and an operation to perform on them. The supported operations are ...

  •  Calculator - switch

    In this C# programming exercise, you need to create a program that asks the user for two numbers and a mathematical operation to perform between them. Supporte...

  •  Double

    In this C# programming exercise, you need to create a program that calculates the perimeter, area, and diagonal of a rectangle, given its widt...

  •  Calculate values of a function

    In this C# programming exercise, you need to create a program that calculates and displays certain values of the function y = x² - 2x + 1, using integer numbers for ...