Determinant of a matrix in Java
In this post, you will learn how to find the determinant of a 2x2 matrix and a 3x3 matrix using the Java programming language.
The determinant of a matrix is a number that is specially defined only for square matrices. Determinants are mathematical objects that are very useful in the analysis and solution of systems of linear equations. The determinant is useful for solving linear equations, capturing how linear transformation changes area or volume. Determinants also have wide applications in engineering, science, social science, and economics as well.
The determinant of a matrix A is denoted det(A), det A, or |A|.
Java Program to find the determinant of a Matrix 2x2
The given Java program allows the user to enter the rows and columns elements of a 2 * 2 Matrix. Then, we apply the determinant formula to calculate the given determinant of the matrix.
import java.util.Scanner;
public class DeterminantOfMatrix {
private static Scanner sc;
public static void main(String[] args) {
int[][] arr = new int[2][2];
int rows, cols, determinant = 0;
sc= new Scanner(System.in);
System.out.println("\n Please Enter the Matrix Items : ");
for(rows = 0; rows < 2; rows++) {
for(cols = 0; cols < 2; cols++) {
arr[rows][cols] = sc.nextInt();
}
}
determinant = (arr[0][0] * arr[1][1]) - (arr[0][1] * arr[1][0]);
System.out.println("The Determinant of 2 * 2 Matrix = " + determinant );
}
}
Output of the above code:
Please Enter the Matrix Items :
6 3
6 21
The Determinant of 2 * 2 Matrix = 108
Java Program to find the determinant of a Matrix 3x3
The given Java program allows the user to enter the rows and columns elements of a 3 * 3 Matrix. Then, we apply the determinant formula to calculate the given determinant of the matrix.
import java.util.Scanner;
public class DeterminantOfMatrix {
private static Scanner sc;
public static void main(String[] args) {
int[][] arr = new int[3][3];
int rows, columns, x, y, z, determinant = 0;
sc= new Scanner(System.in);
System.out.println("\n Please Enter the 3 * 3 Matrix Elements: ");
for(rows = 0; rows < 3; rows++) {
for(columns = 0; columns < 3; columns++) {
arr[rows][columns] = sc.nextInt();
}
}
x = (arr[1][1] * arr[2][2]) - (arr[2][1] * arr[1][2]);
y = (arr[1][0] * arr[2][2]) - (arr[2][0] * arr[1][2]);
z = (arr[1][0] * arr[2][1]) - (arr[2][0] * arr[1][1]);
determinant = (arr[0][0] * x)- (arr[0][1] * y) + (arr[0][2] * z);
System.out.println("The Determinant of 3 * 3 Matrix = " + determinant );
}
}
Output of the above code:
Please Enter the 3 * 3 Matrix Elements:
7 8 2
8 4 1
4 6 3
The Determinant of 3 * 3 Matrix = -54
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java