Monday, 12 January 2015

Java Program to find the lower triangular form for a given matrix


This is a Java program which display the lower triangular form of a given matrix.

To display the lower triangular form of a given matrix we make all the elements above the diagonal row as zero.





See the figure below,

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class LowerTriangularMatrix {
 public static void main(String[] args) {
  int rows, columns, i, j;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter order of matrix :  \t");
  rows = scan.nextInt();
  columns = scan.nextInt();
  int matrix[][] = new int[rows][columns];
  System.out.print("Enter elements of the matrix :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++) {
    System.out.print("Enter element m" + (i + 1) + (j + 1) + ":\t");
    matrix[i][j] = scan.nextInt();
   }
  }
  scan.close();
  System.out.print("\n\nMATRIX is :\n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++)
    System.out.print(matrix[i][j] + "\t");

   System.out.print("\n");
  }

  System.out.print("\n\nIts Lower Triangular form is: \n");
  for (i = 0; i < rows; i++) {
   for (j = 0; j < columns; j++) {
    if (i >= j)
     System.out.print(matrix[i][j] + "\t");
    else
     System.out.print(0 + "\t");
   }
   System.out.print("\n");
  }
 }
}

OUTPUT : 
Java - Lower Triangular Matrix