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
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 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();
}
}
}
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#.
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...
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...
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 ...
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 -...
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....
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...