Monday, 12 January 2015

Java Pogram to find the upper triangular form for a given matrix.



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

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




See the figure below,


PROGRAM  : 
package codingcorner.in;

import java.util.Scanner;

public class UpperTriangularMatrix {
 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 Upper 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 -  Upper Triangular Form