# C# program to Illustrate Upper Triangular Matrix

CsharpServer Side ProgrammingProgramming

#### CSharp Online Training

59 Lectures 6.5 hours

For the upper triangular matrix, set all the elements below the main diagonal to zero.

Set the following condition −

if (i <= j)
Console.Write(A[i, j] + "\t");
else
Console.Write("0\t");

Above condition will set 0 to the matrix elements below the main diagonal.

## Example

You can try to run the following code to display an upper triangular matrix.

Live Demo

using System;
using System.Linq;
class Demo {
static void Main() {
int m, n, i, j;
Console.Write("Enter number of rows and columns of the matrix ");
int[,] A = new int[10, 10];
Console.Write("\nEnter elements: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
A[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("\nUpper Triangular Matrix ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
Console.Write(A[i, j] + "\t");
}
Console.WriteLine();
}
for (i = 0; i < m; i++) {
Console.Write("\n");
for (j = 0; j < 3; j++) {
if (i >= j)
Console.Write(A[i, j] + "\t");
else
Console.Write("0\t");
}
}
}
Enter number of rows and columns of the matrix
Upper Triangular Matrix