Java Program to Compute 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 = 0Problem 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.

//Java Program to Find the Modulus of a 
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
public class ModulusOfAMatrix {
    // Function to read array elements and calculate the determinant
    public static void main(String[] args) {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        int order=3;
        int[][] matrix=new int[3][3];
        System.out.println("Enter the elements of 3x3 matrix");
        int i,j;
        for(i=0;i<matrix.length;i++){
            for(j=0;j<matrix[i].length;j++){
                try{
                    matrix[i][j]=Integer.parseInt(br.readLine());
                }
                catch(Exception e){
                    System.out.println("An error occured. Please retry");
                    return;
                }
            }
        }
        int determinant,x,y,z;
        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;
        System.out.println("The modulus of the given matrix is "+ determinant);
 
    }
}

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).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

Related posts:

Spring Cloud Bus
How to Round a Number to N Decimal Places in Java
JUnit 5 for Kotlin Developers
Java Program to Implement TreeMap API
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement SynchronosQueue API
Java Program to Implement Interval Tree
Java Program to Check Whether Graph is DAG
Limiting Query Results with JPA and Spring Data JPA
Java – Delete a File
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Implement CopyOnWriteArrayList API
Spring’s RequestBody and ResponseBody Annotations
Constructor Dependency Injection in Spring
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Quick sort
Documenting a Spring REST API Using OpenAPI 3.0
wait() and notify() Methods in Java
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Prevent Brute Force Authentication Attempts with Spring Security
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Lớp LinkedHashMap trong Java
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Get the workstation name or IP
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
The Spring @Controller and @RestController Annotations
Handle EML file with JavaMail
Reversing a Linked List in Java
Testing an OAuth Secured API with Spring MVC
Java Program to Check Multiplicability of Two Matrices