Java Program to Calculate Determinant of a Matrix


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
 
public class Main {
    // A Function to read array elements from user and calculate the determinant
    public static void main(String[] args) {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        
        
        int[][] matrx=new int[3][3];
        System.out.println("Enter the elements of 3x3 matrix");
        int x,y;
        for(x=0;x<matrx.length;x++){
            for(y=0;y<matrx[x].length;y++){
                try{
                    matrx[x][y]=Integer.parseInt(br.readLine());
                }
                catch(Exception e){
                    System.out.println("An error occured. Please retry");
                    return;
                }
            }
        }
        int matrix_determinant,i,j,k;
        i=(matrx[0][0] * (matrx[1][1] * matrx[2][2]
                       - matrx[1][2] * matrx[2][1]));
        j=(matrx[0][1] * (matrx[1][0] * matrx[2][2]
                       - matrx[1][2] * matrx[2][0]));
        k=(matrx[0][2] * (matrx[1][0] * matrx[2][1]
                       - matrx[1][1] * matrx[2][0]));
        matrix_determinant= i - j + k;
        System.out.println("The determinant of the  matrix is: "+ matrix_determinant);
 
    }
}

 

Output:-

 


 

Leave a Reply

Your email address will not be published. Required fields are marked *