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.
import java.util.Scanner;
public class Lower_Matrix
{
public static void main(String args[])
{
int a[][] = new int[5][5];
System.out.println("Enter the order of your Matrics ");
System.out.println("Enter the rows:");
Scanner sc = new Scanner(System.in);
int n = sc.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 ss = new Scanner(System.in);
a[i][j] = ss.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:
$ 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.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Programming Books
- Buy Java Books
- Practice BCA MCQs
- Apply for Information Technology Internship
- Apply for Java Internship