Home / JAVA / program for display lower triangular matrix in java

program for display lower triangular matrix in java

What is Lower Triangular Matrix? Lower Triangular Matrix is if all the elements above the main diagonal of a square matrix are zero then the matrix is called ‘Lower Triangular Matrix’.

Example:

lower triangular matrix

 

The matrix contains all the elements above main diagonal is zero, so this matrix

 begin{bmatrix} 1 & 0 & 0 \ 2 & 8 & 0 \ 4 & 9 & 7 \ end{bmatrix}

is lower triangular matrix.

 

import java.util.*;

public class LowerTriangularMatrix
{
   public static void main(String args[])
   {
       int arr[][] = new int[10][10];
       System.out.println("Enter the order of your Matrics ");
       System.out.println("Enter the rows:");
       Scanner sy = new Scanner(System.in);
       int n = sy.nextInt();
       System.out.println("Enter the columns:");
       Scanner s = new Scanner(System.in);
       int m = s.nextInt();
       if(n == m)
       {        
           System.out.println("Enter your elements:");
           for(int i = 0; i < n; i++)
           {
                 for(int j = 0; j < n; j++)
                 {      
                      Scanner sx = new Scanner(System.in);
                      a[i][j] = sx.nextInt();
                      System.out.print(" ");
                 }
            }
            System.out.println("You have entered:");
            for(int i=0; i<n; i++)
            {
                for(int j=0;j<n;j++)
                {      
                     System.out.print(a[i][j] + " ");
                }
                System.out.println("");
             }
             System.out.println("The Lower Triangular Matrix is:");
             for(int i=0;i<n;i++)
             {
                 for(int j=0;j<n;j++)
                 {
                     if(i>=j)
                    {
                        System.out.print(a[i][j] +" ");
                    }
                    else
                    {
                        System.out.print("0"+" ");
                    } 
                }
             System.out.println("");
             }
         }
          else
         {
             System.out.println("you have entered improper order");
         }
    }
}

Output:

Enter the order of your Matrics
Enter the rows:
3
Enter the columns:
3
Enter your elements:
1
2
3
4
5
6
7
8
9
You have entered:
1 2 3
4 5 6
7 8 9
The Lower Triangular Matrix is:
1 0 0
4 5 0
7 8 9

So this was all the complete code for what your were searching –

How to display Lower triangle matrix in C, JAVA program

 

Check Also

Java Program to find Mobius Function of number

Mobius Function Java Program:  The MOBIUS function µ(n) for a natural number N is defined as ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.