Home »
.Net »
C# Programs
C# program to print the lower triangular matrix
Here, we are going to learn how to print the lower triangular matrix in C#?
Submitted by Nidhi, on November 05, 2020 [Last updated : March 19, 2023]
Printing Lower Triangular Matrix
Here, we read a matrix from the user and then print the lower triangular matrix on the console screen.
C# code to print lower triangular matrix
The source code to print the lower triangular matrix is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to print lower triangular matrix.
using System;
class MatrixDemo
{
public static void Main(string[] args)
{
int i = 0;
int j = 0;
int row = 3;
int col = 3;
int[,] Matrix = new int[row, col];
Console.Write("Enter the elements of matrix: ");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Matrix[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\nMatrix");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
Console.Write(Matrix[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Lower triangular matrix: ");
for (i = 0; i < row; i++)
{
Console.WriteLine();
for (j = 0; j < col; j++)
{
if (i >= j)
Console.Write(Matrix[i, j] + "\t");
else
Console.Write(" \t");
}
}
Console.WriteLine();
}
}
Output
Enter the elements of matrix: 1
2
3
4
5
5
6
7
8
Matrix
1 2 3
4 5 5
6 7 8
Lower triangular matrix:
1
4 5
6 7 8
Press any key to continue . . .
Explanation
In the above program, we created a class MatrixDemo that contains the Main() method. Here, we read a 3X3 matrix and then print the lower triangular matrix on the console screen using the below code.
for (i = 0; i < row; i++)
{
Console.WriteLine();
for (j = 0; j < col; j++)
{
if (i >= j)
Console.Write(Matrix[i, j] + "\t");
else
Console.Write(" \t");
}
}
C# Basic Programs »