TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Display The Lower Triangular Matrix

Program to Display The Lower Triangular Matrix on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

Program to display the lower triangular matrix

Explanation

In this program, we need to display the lower triangular matrix.

Lower Triangular Matrix

Lower triangular matrix is a square matrix in which all the elements above the principle diagonal will be zero. To find the lower triangular matrix, a matrix needs to be a square matrix that is, number of rows and columns in the matrix needs to be equal. Dimensions of a typical square matrix can be represented by n x n.

Program to display the lower triangular matrix

Consider the above example, principle diagonal element of given matrix is (1, 6, 6). All the elements above diagonal needs to be made zero. In our example, those elements are at positions (1,2), (1,3) and (2,3). To convert given matrix into the lower triangular matrix, loop through the matrix and set the values of the element to zero where column number is greater than row number.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array and store it in variables rows and cols respectively.
  3. If the number of rows are not equal to the number of columns, then the given matrix is not a square matrix. Hence, given matrix cannot be converted to the lower triangular matrix. Display the error message.
  4. If rows = cols, traverse the array a using two loops where outer loop represents the rows, and inner loop represents the columns of the array a. To convert given matrix to lower triangular matrix, set the elements of the array to 0 where (j > i) that is, the column number is greater than row number.
  5. Display the resulting matrix.

Solution

Python

#Initialize matrix a
a = [   
        [1, 2, 3],
        [8, 6, 4],
        [4, 5, 6]
    ];
 
#Calculates number of rows and columns present in given matrix
rows = len(a);
cols = len(a[0]);
 
if(rows != cols):
    print("Matrix should be a square matrix");
else:
    #Performs required operation to convert given matrix into lower triangular matrix
    print("Lower triangular matrix: ");
    for i in range(0, rows):
        for j in range(0, cols):
            if(j > i):
                print("0"),
            else:
                print(a[i][j]),
    
        print(" ");

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

C

#include <stdio.h>
 
int main()
{
    int rows, cols;
        
    //Initialize matrix a
    int a[][3] = {   
                    {1, 2, 3},
                    {8, 6, 4},
                    {4, 5, 6}
                 };
    
    //Calculates number of rows and columns present in given matrix
    rows = (sizeof(a)/sizeof(a[0]));
    cols = (sizeof(a)/sizeof(a[0][0]))/rows;
    
    if(rows != cols){
        printf("Matrix should be a square matrix\n");
    }
    else{
        //Performs required operation to convert given matrix into lower triangular matrix
        printf("Lower triangular matrix: \n");
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
              if(j > i)
                printf("0 ");
              else
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

JAVA

public class LowerTriangular
{
    public static void main(String[] args) {
        int rows, cols;
        
        //Initialize matrix a
        int a[][] = {   
                        {1, 2, 3},
                        {8, 6, 4},
                        {4, 5, 6}
                    };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.length;
          cols = a[0].length;
        
          if(rows != cols){
              System.out.println("Matrix should be a square matrix");
          }
          else {
              //Performs required operation to convert given matrix into lower triangular matrix
              System.out.println("Lower triangular matrix: ");
              for(int i = 0; i < rows; i++){
                  for(int j = 0; j < cols; j++){
                    if(j > i)
                      System.out.print("0 ");
                    else
                      System.out.print(a[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

C#

using System;
                    
public class LowerTriangular
{
    public static void Main()
    {
        int rows, cols;
        
        //Initialize matrix a
        int[,] a = {   
                        {1, 2, 3},
                        {8, 6, 4},
                        {4, 5, 6}
                   };
          
          //Calculates number of rows and columns present in given matrix
          rows = a.GetLength(0);
          cols = a.GetLength(1);
        
        if(rows != cols){
            Console.WriteLine("Matrix should be a square matrix");
        }
        else {
            //Performs required operation to convert given matrix into lower triangular matrix
            Console.WriteLine("Lower triangular matrix: ");
            for(int i = 0; i < rows; i++){
                for(int j = 0; j < cols; j++){
                  if(j > i)
                    Console.Write("0 ");
                  else
                    Console.Write(a[i,j] + " ");
                }
                Console.WriteLine();
            }
        }
    }
}

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize matrix a
$a = array(   
            array(1, 2, 3),
            array(8, 6, 4),
            array(4, 5, 6)
          );
 
//Calculates number of rows and columns present in given matrix
$rows = count($a);
$cols = count($a[0]);
 
if($rows != $cols){
    print("Matrix should be a square matrix <br>");
}
else {
    //Performs required operation to convert given matrix into lower triangular matrix
    print("Lower triangular matrix: <br>");
    for($i = 0; $i < $rows; $i++){
        for($j = 0; $j < $cols; $j++){
          if($j > $i)
            print("0 ");
          else
            print($a[$i][$j] . " ");
        }
        print("<br>");
    }
}
?>
</body>
</html>

Output:

Lower triangular matrix: 
1 0 0 
8 6 0 
4 5 6 

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf