Home > Backend Development > C#.Net Tutorial > C# program to illustrate upper triangular matrix

C# program to illustrate upper triangular matrix

WBOY
Release: 2023-09-08 10:13:14
forward
1494 people have browsed it

说明上三角矩阵的 C# 程序

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

Set the following conditions −

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

The above conditions will set the matrix elements below the main diagonal to 0.

Example

You can try running 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 ");
      m = Convert.ToInt16(Console.ReadLine());
      n = Convert.ToInt16(Console.ReadLine());
      int[,] A = new int[10, 10];
      Console.Write("Enter elements: ");
      for (i = 0; i < m; i++) {
         for (j = 0; j < n; j++) {
            A[i, j] = Convert.ToInt16(Console.ReadLine());
         }
      }
      Console.WriteLine("Upper 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("");
         for (j = 0; j < 3; j++) {
            if (i >= j)
               Console.Write(A[i, j] + "\t");
            else
               Console.Write("0\t");
         }
      }
      Console.ReadLine();
   }
}
Copy after login

Output

Enter number of rows and columns of the matrix  
Enter elements:  
Upper Triangular Matrix 
Copy after login

The above is the detailed content of C# program to illustrate upper triangular matrix. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template