Users Online

· Guests Online: 43

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Display Lower Triangular Matrix

C# Program to Display Lower Triangular Matrix

 

This is a C# Program to display a lower triangular matrix.

Problem Description

This C# Program Displays Lower Triangular Matrix.

Problem Solution

A square matrix is lower triangular matrix if all its entries above the main diagonal are zero.

Program/Source Code

Here is source code of the C# Program to Display Lower Triangular Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Display Lower Triangular Matrix 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
    class Program
    {
        int x;
        public static void Main(string[] args)
        {
            int m, n, i, j;
 
            Console.Write("Enter Number Of Rows And Columns Of Matrices A and B : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The First Matrix : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }          
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
 
            Console.WriteLine("\n Setting Zero to illustrate " + 
                              "Lower Triangular Matrix\n");
            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");
                }
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, we are reading the order of the matrix row and column using ‘m’ and ‘n’ variables respectively. Using for loop we are entering the coefficient value of the matrix to the variable A[i,j] to the index of m*n matrix. To display lower triangular matrix two for loops are used.

In first for loop, initialize the value of ‘i’ variable to zero and check the condition that the value of ‘i’ variable is less than the value of ‘row’ variable. In another for loop initialize the value of ‘j’ variable to zero and check the condition that the value of ‘j’ variable is less than the value of ‘column’ variable. Print the value of the lower triangular matrix.
Runtime Test Cases
 
Enter Number Of Rows And Columns Of Matrices A and B : 3  3
Enter the First Matrix : 
1 2 3
2 3 4
3 4 5
Matrix A :
1 2 3
2 3 4
3 4 5
Setting Zero to illustrate Lower Triangular Matrix :
1 0 0
2 3 0
3 4 5

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.76 seconds
10,846,088 unique visits