- Open Visual Studio or any C# IDE of your choice.
- Copy and paste the code from the provided source file into the IDE.
- Compile and run the program.
- Follow the on-screen instructions to input the desired triangle size.
The program draws two types of triangles using the #
symbol:
- Ascending triangle: Starts with one
#
and increases in size to the specified height. - Descending triangle: Starts at the full height and decreases in size down to one
#
.
The program ensures only valid positive integer inputs are accepted and notifies the user in case of invalid input.
This program prompts the user to input a positive integer value representing the size of the triangle. It then generates two visual patterns:
- An ascending triangle, which starts with a single
#
and grows in size line-by-line up to the specified height. - A descending triangle, which starts at the full height with
#
symbols and reduces to a single#
.
If the input is invalid or not greater than zero, an error message is displayed, and the program terminates.
- Input validation: Ensures that the size of the triangle is a positive integer.
- Dynamic pattern creation: The triangles are drawn based on user input.
- Error handling: Provides clear feedback for invalid inputs.
- The user is prompted to input the triangle size.
- If the input is invalid or not greater than zero, the program displays an error message and terminates.
- If the input is valid:
- Ascending triangle:
- Uses nested
for
loops to dynamically generate the pattern by incrementing the number of#
symbols in each subsequent line.
- Uses nested
- Descending triangle:
- Uses nested
for
loops to dynamically generate the pattern by decrementing the number of#
symbols in each subsequent line.
- Uses nested
- Ascending triangle:
-
Input Handling:
- The size is read using
Console.ReadLine()
and validated usingint.TryParse
. - Checks that the input is greater than zero before proceeding.
- The size is read using
-
Pattern Drawing:
- Ascending triangle: The outer loop iterates over the height, while the inner loop determines the number of
#
symbols in each line. - Descending triangle: The outer loop iterates over the height, while the inner loop reduces the number of
#
symbols in each line.
- Ascending triangle: The outer loop iterates over the height, while the inner loop determines the number of
-
Error Handling:
- Ensures the user enters valid positive integers. Displays an appropriate message for invalid inputs.
This triangle drawing program effectively demonstrates fundamental programming concepts such as loops, input validation, and dynamic output generation. The patterns are simple yet versatile, making the program a great introduction to nested loops and user-defined outputs.