Java Program to Display Lower Triangular Matrix

This is a Java Program to Display Lower Triangular Matrix.

Enter the elements of array as input. We use loops and if-else conditions to print only the diagonals and the elements below diagonal as it is and rest of the elements as zeros.

Here is the source code of the Java Program to Display Lower Triangular Matrix. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Lower_Matrix
  3. {
  4. public static void main(String args[])
  5.     {
  6.         int a[][] = new int[5][5];
  7.         System.out.println("Enter the order of your Matrics ");
  8.         System.out.println("Enter the rows:");
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         System.out.println("Enter the columns:");
  12.         Scanner s = new Scanner(System.in);
  13.         int m = s.nextInt();
  14.         if(n == m)
  15.          {        
  16.              System.out.println("Enter your elements:");
  17.               for(int i = 0; i < n; i++)
  18.             {
  19.                 for(int j = 0; j < n; j++)
  20.                 {      
  21.  
  22.                      Scanner ss = new Scanner(System.in);
  23.                      a[i][j] = ss.nextInt();
  24.                      System.out.print(" ");
  25.                  }
  26.             }
  27.               System.out.println("You have entered:");
  28.               for(int i=0; i<n; i++)
  29.                {
  30.                 for(int j=0;j<n;j++)
  31.                 {      
  32.                      System.out.print(a[i][j] + " ");
  33.                  }
  34.                 System.out.println("");
  35.                }
  36.               System.out.println("The Lower Triangular Matrix is:");
  37.               for(int i=0;i<n;i++)
  38.                {
  39.                 for(int j=0;j<n;j++)
  40.                  {
  41.                   if(i>=j)
  42.                    {
  43.                      System.out.print(a[i][j] +" ");
  44.                    }
  45.                 else
  46.                 {
  47.                     System.out.print("0"+" ");
  48.                 } 
  49.               }
  50.             System.out.println("");
  51.            }
  52.         }
  53.          else
  54.         {
  55.             System.out.println("you have entered improper order");
  56.         }
  57.    }
  58. }

Output:

$ javac Lower_Matrix.java
$ java Lower_Matrix
 
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

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

👉 For weekly programming practice and certification updates, join Sanfoundry’s official WhatsApp & Telegram channels
advertisement
Manish Bhojasia – Founder & CTO at Sanfoundry

I’m Manish, Founder & CTO at Sanfoundry, with 25+ years of experience across Linux systems, SAN technologies, advanced C programming, and building large-scale, performance-driven learning and certification platforms focused on clear skill validation.

LinkedIn  ·  YouTube MasterClass  ·  Telegram Classes  ·  Career Guidance & Conversations