From 0561cac52fd5f83d124010f71d131c905c04b505 Mon Sep 17 00:00:00 2001 From: leenajilain7 Date: Sat, 2 Oct 2021 22:40:12 +0530 Subject: [PATCH 1/2] added finding lower traingualr matrix using java --- LowerTraingular.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 LowerTraingular.java diff --git a/LowerTraingular.java b/LowerTraingular.java new file mode 100644 index 0000000..6b8890e --- /dev/null +++ b/LowerTraingular.java @@ -0,0 +1,33 @@ +public class LowerTriangular +{ + public static void main(String[] args) { + int rows, cols; + //Initialize matrix a + int a[][] = { + {1, 2, 3}, + {8, 6, 4}, + {4, 5, 6} + }; + + //Calculates number of rows and columns present in given matrix + rows = a.length; + cols = a[0].length; + + if(rows != cols){ + System.out.println("Matrix should be a square matrix"); + } + else { + //Performs required operation to convert given matrix into lower triangular matrix + System.out.println("Lower triangular matrix: "); + for(int i = 0; i < rows; i++){ + for(int j = 0; j < cols; j++){ + if(j > i) + System.out.print("0 "); + else + System.out.print(a[i][j] + " "); + } + System.out.println(); + } + } + } +} From f5bb7af87e340909d224aad9682c5798b7fe48c9 Mon Sep 17 00:00:00 2001 From: leenajilain7 Date: Sat, 2 Oct 2021 22:42:42 +0530 Subject: [PATCH 2/2] added checking equal matrix --- EqualMatrix.java | 51 ++++++++++++++++++++++++++++++++++++++++++++ LowerTraingular.java | 33 ---------------------------- 2 files changed, 51 insertions(+), 33 deletions(-) create mode 100644 EqualMatrix.java delete mode 100644 LowerTraingular.java diff --git a/EqualMatrix.java b/EqualMatrix.java new file mode 100644 index 0000000..d8dfc09 --- /dev/null +++ b/EqualMatrix.java @@ -0,0 +1,51 @@ +public class EqualMatrix +{ + public static void main(String[] args) { + int row1, col1, row2, col2; + boolean flag = true; + + //Initialize matrix a + int a[][] = { + {1, 2, 3}, + {8, 4, 6}, + {4, 5, 7} + }; + + //Initialize matrix b + int b[][] = { + {1, 2, 3}, + {8, 4, 6}, + {4, 5, 7} + }; + + //Calculates the number of rows and columns present in the first matrix + + row1 = a.length; + col1 = a[0].length; + + //Calculates the number of rows and columns present in the second matrix + + row2 = b.length; + col2 = b[0].length; + + //Checks if dimensions of both the matrices are equal + if(row1 != row2 || col1 != col2){ + System.out.println("Matrices are not equal"); + } + else { + for(int i = 0; i < row1; i++){ + for(int j = 0; j < col1; j++){ + if(a[i][j] != b[i][j]){ + flag = false; + break; + } + } + } + + if(flag) + System.out.println("Matrices are equal"); + else + System.out.println("Matrices are not equal"); + } + } +} \ No newline at end of file diff --git a/LowerTraingular.java b/LowerTraingular.java deleted file mode 100644 index 6b8890e..0000000 --- a/LowerTraingular.java +++ /dev/null @@ -1,33 +0,0 @@ -public class LowerTriangular -{ - public static void main(String[] args) { - int rows, cols; - //Initialize matrix a - int a[][] = { - {1, 2, 3}, - {8, 6, 4}, - {4, 5, 6} - }; - - //Calculates number of rows and columns present in given matrix - rows = a.length; - cols = a[0].length; - - if(rows != cols){ - System.out.println("Matrix should be a square matrix"); - } - else { - //Performs required operation to convert given matrix into lower triangular matrix - System.out.println("Lower triangular matrix: "); - for(int i = 0; i < rows; i++){ - for(int j = 0; j < cols; j++){ - if(j > i) - System.out.print("0 "); - else - System.out.print(a[i][j] + " "); - } - System.out.println(); - } - } - } -}