Triangle Patterns in C#

A number triangle is a type of pattern that displays a series of numbers in a triangular format. In C#, there are various methods to create a number triangle. Here are some possible methods:

Using nested loops

One way to create a number triangle in C# is by using nested loops.

using System; public class Program { public static void Main() { int numberOfRows = 5; int currentNumber = 1; for (int i = 1; i <= numberOfRows; i++) { for (int j = 1; j <= i; j++) { Console.Write(currentNumber + " "); currentNumber++; } Console.WriteLine(); } } }
//Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Explanation:

First declare two variables n and num. n represents the number of rows and want to display, while num is the starting number for the triangle. Then use a for loop to iterate through each row of the triangle. The outer loop runs n times, one for each row. Within the outer loop, use another for loop to iterate through each column of the triangle. The inner loop runs i times, where i represents the current row number. Inside the inner loop, print the current value of num, followed by a space. Then increment num by 1. After the inner loop finishes, print a newline character to move to the next row.

Using recursion

Following is an example of how to generate a number triangle using recursion in C#:

using System; class Program { static void Main(string[] args) { int rows = 5; // number of rows in the triangle // loop through each row and print it for (int i = 0; i < rows; i++) { for (int j = 0; j <= i; j++) { Console.Write(TriangleNumber(i, j) + " "); } Console.WriteLine(); } } static int TriangleNumber(int row, int col) { if (col == 0 col == row) { return 1; // base case } else { return TriangleNumber(row - 1, col - 1) + TriangleNumber(row - 1, col); // recursive case } } }
//Output: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Above code defines a TriangleNumber method that takes two arguments: the row number and the column number of a given position in the triangle. The method uses recursion to calculate the value at that position based on the values of the two positions above it. The main method then loops through each row and column of the triangle, calling the TriangleNumber method to generate the value at each position and printing it out. This produces the output of the number triangle:


draw number triangle in C#

Note that the TriangleNumber method uses two base cases: when the column is 0 or when the column is equal to the row number. In both cases, the value at that position 1.

Using a single loop and arithmetic progression

Another method to create a number triangle is by using a single loop and arithmetic progression.

using System; class Program { static void Main(string[] args) { int n = 5; // number of rows int num = 1; // starting number for (int i = 1; i <= n; i++) { int rowSum = (i * (i + 1)) / 2; // calculate sum of current row for (int j = 1; j <= i; j++) { Console.Write(num + " "); num++; } Console.WriteLine("= " + rowSum); } } }
//Output: 1 = 1 2 3 = 5 4 5 6 = 15 7 8 9 10 = 34 11 12 13 14 15 = 65

Explanation:

This method is similar to the nested loop method, but instead of using two loops, use a single loop to iterate through each row. Calculate the sum of the current row using the formula rowSum = (i * (i + 1)) / 2, where i is the current row number. Within the loop, print the current value of num, followed by a space. Then increment num by 1. After the loop finishes, print the sum of the current row.