Q. Write a program to print Lower triangular matrix of an array.



Lower triangular matrix :-A square matrix is called lower triangular if all the entries above the main diagonal are zero.
For Example :-
Input = matrix[3][3] =
{1 2 3
4 5 6
7 8 9}

Output = Lower :
1 0 0
4 5 0
7 8 9

Lower Triangular Matrix Algorithm

START
Step 1 → Find the Index position of row and column.
Step 2 → If column position is greater than row position, then make that position 0.
STOP


Lower Triangular Matrix Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main() {
    int rows=3, cols=3, i, j, matrix[3][3]={1,2,3,4,5,6,7,8,9};
    printf("The Lower Triangular Matrix is: ");
    for(i = 0; i < rows; i++){
    printf("\n");
    for(j = 0; j < cols; j++){
    if(i >= j){
    printf("%d\t ", matrix[i][j]);
    }
    else{
    printf("0");
    printf("\t");
    }
    }
    }
    return 0;
    }
    
    #include <iostream>
    using namespace std; 
    // lower triangular matrix 
    void lower(int matrix[3][3], int row, int col) 
    { 
    int i, j; 
    for (i = 0; i < row; i++) 
    { 
    for (j = 0; j < col; j++) 
    { 
    if (i < j) 
    { 
    cout << "0" << " "; 
    } 
    else
    cout << matrix[i][j] << " "; 
    } 
    cout << endl; 
    } 
    } 
    // Driver Code 
    int main() 
    { 
    int matrix[3][3] = {{1, 2, 3},  
    {4, 5, 6},  
    {7, 8, 9}}; 
    int row = 3, col = 3; 
    cout << "The Lower Triangular Matrix is: \n"; 
    lower(matrix, row, col); 
    return 0; 
    } 
    
    class LFC 
    { 
    // method to form lower  
    // triangular matrix 
    static void lower(int matrix[][],  
    int row, int col) 
    { 
    int i, j; 
    for (i = 0; i < row; i++) 
    { 
    for (j = 0; j < col; j++) 
    { 
    if (i < j) 
    { 
    System.out.print("0" + " "); 
    } 
    else
    System.out.print(matrix[i][j] + " "); 
    } 
    System.out.println(); 
    } 
    } 
    public static void main(String args[]) 
    { 
    int matrix[][] = {{1, 2, 3},  
    {4, 5, 6},  
    {7, 8, 9}}; 
    int row = 3, col = 3; 
    System.out.println("The Lower Triangular Matrix is: "); 
    lower(matrix, row, col); 
    } 
    } 
    
    def lower(matrix, row, col): 
    for i in range(0, row): 
    for j in range(0, col): 
    if (i < j): 
    print("0", end = " "); 
    else: 
    print(matrix[i][j],  
    end = " " ); 
    print(" "); 
    matrix = [[1, 2, 3],  
    [4, 5, 6],  
    [7, 8, 9]]; 
    row = 3; 
    col = 3; 
    print("The Lower Triangular Matrix is: "); 
    lower(matrix, row, col);  
    
    using System; 
    class LFC 
    { 
    // method to form lower  
    // triangular matrix 
    static void lower(int [,]matrix,  
    int row, int col) 
    { 
    int i, j; 
    for (i = 0; i < row; i++) 
    { 
    for (j = 0; j < col; j++) 
    { 
    if (i < j) 
    { 
    Console.Write("0" + " "); 
    } 
    else
    Console.Write(matrix[i, j] + " "); 
    } 
    Console.WriteLine(); 
    } 
    } 
    static public void Main () 
    { 
    int [,]matrix = {{1, 2, 3},  
    {4, 5, 6},  
    {7, 8, 9}}; 
    int row = 3, col = 3; 
    Console.WriteLine("The Lower Triangular Matrix is: "); 
    lower(matrix, row, col); 
    } 
    } 
    
    // Function to form  
    // lower triangular matrix 
    function lower($matrix, $row, $col) 
    { 
    $i; $j; 
    for ($i = 0; $i < $row; $i++) 
    { 
    for ($j = 0; $j < $col; $j++) 
    { 
    if ($i < $j) 
    { 
    echo "0" , " "; 
    } 
    else
    echo $matrix[$i][$j] , " "; 
    } 
    echo "\n"; 
    } 
    } 
    $matrix = array (array (1, 2, 3), 
    array (4, 5, 6), 
    array (7, 8, 9)); 
    $row = 3; $col = 3; 
    echo "The Lower Triangular Matrix is: \n"; 
    lower($matrix, $row, $col); 
    
    #include <stdio.h>
    int main() {
    int rows=3, cols=3, i, j, matrix[3][3]={1,2,3,4,5,6,7,8,9};
    printf("\n The Lower Triangular Matrix is: ");
    for(i = 0; i < rows; i++){
    printf("\n");
    for(j = 0; j < cols; j++){
    if(i >= j){
    printf("%d\t ", matrix[i][j]);
    }
    else{
    printf("0");
    printf("\t");
    }
    }
    }
    return 0;
    }
    

    Output

    The Lower Triangular Matrix is: 
    1  0  0
    4  5  0
    7  8  9
    

    Recommended Programs

       Program to print upper triangular matrix.

       Program to print half pyramid pattern.