Java Program to Find the Determinant of a Matrix

«
»

This is the Java Program to Find the Modulus of a Matrix.

Problem Description

Given a square matrix, find and print the modulus(determinant) of the matrix.
Example:
Matrix:
1 2 3
4 5 6
7 8 9

Output:
Modulus = 0

advertisement
Problem Solution

The algorithm for calculating modulus of 3*3 matrix is
x=(matrix[0][0] * (matrix[1][1] * matrix[2][2] – matrix[1][2] * matrix[2][1]));
y=(matrix[0][1] * (matrix[1][0] * matrix[2][2] – matrix[1][2] * matrix[2][0]));
z=(matrix[0][2] * (matrix[1][0] * matrix[2][1] – matrix[1][1] * matrix[2][0]));

determinant= x – y + z;

Program/Source Code

Here is the source code of the Java Program to Find the Modulus of a Matrix. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

advertisement
advertisement
  1.  
  2. //Java Program to Find the Modulus of a 
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ModulusOfAMatrix {
  8.     // Function to read array elements and calculate the determinant
  9.     public static void main(String[] args) {
  10.         BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  11.         int order=3;
  12.         int[][] matrix=new int[3][3];
  13.         System.out.println("Enter the elements of 3x3 matrix");
  14.         int i,j;
  15.         for(i=0;i<matrix.length;i++){
  16.             for(j=0;j<matrix[i].length;j++){
  17.                 try{
  18.                     matrix[i][j]=Integer.parseInt(br.readLine());
  19.                 }
  20.                 catch(Exception e){
  21.                     System.out.println("An error occured. Please retry");
  22.                     return;
  23.                 }
  24.             }
  25.         }
  26.         int determinant,x,y,z;
  27.         x=(matrix[0][0] * (matrix[1][1] * matrix[2][2]
  28.                        - matrix[1][2] * matrix[2][1]));
  29.         y=(matrix[0][1] * (matrix[1][0] * matrix[2][2]
  30.                        - matrix[1][2] * matrix[2][0]));
  31.         z=(matrix[0][2] * (matrix[1][0] * matrix[2][1]
  32.                        - matrix[1][1] * matrix[2][0]));
  33.         determinant= x - y + z;
  34.         System.out.println("The modulus of the given matrix is "+ determinant);
  35.  
  36.     }
  37. }
Program Explanation

1. In function main(), a matrix is entered.
2. Then in variables x, y and z various coefficients are calculated.
3. Finally, the statement (determinant= x – y + z), calculates the determinant and it is displayed.

Time Complexity: O(1).

advertisement
Runtime Test Cases
 
Case 1 (Simple Test Case):
 
Enter the elements of 3x3 matrix
1
2
3
4
5
6
7
8
9
The modulus of the given matrix is 0
 
Case 2 (Simple Test Case - another example):
 
Enter the elements of 3x3 matrix
12
43
5
23
56
7
45
2
65
The modulus of the given matrix is -19598

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

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.