Triangle, Northeast - C# Programming Exercise

In this C# exercise, you are asked to write a program that prompts the user for a width and then displays a triangle where the base has the number of asterisks corresponding to the given width, and each row from top to bottom has a decreasing number of asterisks, preceded by underscores. For example, if the width is 5, the triangle will appear as follows:

**
**
__*
__
____

This exercise is great for practicing string manipulation and the use of loops in C#. Through this program, you will learn how to generate visual patterns using characters and control the number of spaces (represented by underscores) and asterisks in each row. The for loop will be an essential tool for creating the rows of the triangle and decreasing the number of asterisks as you move down the rows.

Such exercises help to improve your ability to work with loops and strings in C#, as well as enhance your logic for generating patterns and repetitive structures in the console.

 Category

Basic Data Types

 Exercise

Triangle, Northeast

 Objective

Write a C# program which asks for a width, and displays a triangle like this one:

Enter the desired width: 5

*****
_****
__***
___**
____*

 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 the desired width of the triangle
        Console.Write("Enter the desired width: ");  // Display prompt for width input
        int width = int.Parse(Console.ReadLine());  // Read the input as an integer

        // Loop through each row of the triangle
        for (int i = 0; i < width; i++)  // Loop through from 0 to (width-1)
        {
            // Print spaces before the asterisks
            for (int j = 0; j < i; j++)  // Loop through the number of spaces to print
            {
                Console.Write("_");  // Print an underscore for each space
            }

            // Print the asterisks
            for (int j = 0; j < (width - i); j++)  // Loop to print the asterisks in each row
            {
                Console.Write("*");  // Print an asterisk
            }

            // Move to the next line after each row
            Console.WriteLine();
        }
    }
}

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

  •  Prime factors

    In this C# exercise, you are asked to write a program that takes a number entered by the user and displays it as a product of its prime factors. Prime factors are numbers th...

  •  If, symbols

    In this C# exercise, you are asked to write a program that prompts the user for a symbol and determines if it is an uppercase vowel, a lowercase vowel, a digit...

  •  Char + for

    In this C# exercise, you are asked to write a program that prints the letters from B to N in uppercase, using a for loop. This type of exercise is perfect for ...

  •  Double, approximation of Pi

    In this C# exercise, you are asked to write a program that calculates an approximation for PI using the alternating fraction series: pi/4 = 1/1 - 1/3 + 1/5 -...

  •  Perimeter Area

    In this C# exercise, you are asked to write a program that calculates the perimeter, area, and diagonal of a rectangle from its width and height....

  •  Hexadecimal and binary

    In this C# exercise, you are asked to write a program that prompts the user for a number and displays it in both hexadecimal and binary. The program should continue r...