forked from SoumyadeepMukherjee/Matrix_Manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inverse.java
27 lines (26 loc) · 922 Bytes
/
Inverse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Scanner;
public class Inverse {
public static void main(String args[]) {
int i, j;
float det, temp;
float mat[][] = new float[2][2];
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements of matrix row wise:");
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j)
mat[i][j] = sc.nextFloat();
det = (mat[0][0] * mat[1][1]) - (mat[0][1] * mat[1][0]);
System.out.println("\ndeterminant = " + det);
temp = mat[0][0];
mat[0][0] = mat[1][1];
mat[1][1] = temp;
mat[0][1] = - mat[0][1];
mat[1][0] = - mat[1][0];
System.out.println("\nInverse of matrix is:");
for(i = 0; i < 2; ++i) {
for(j = 0; j < 2; ++j)
System.out.print((mat[i][j]/det) + " ");
System.out.print("\n");
}
}
}