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.

advertisement
  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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.