Java Program to Find Inverse of a Matrix
Here you will get java program to find inverse of a matrix of order 2×2 and 3×3. We can find inverse of a matrix in following way. First find the determinant of matrix. Calculate adjoint of matrix. Finally divide adjoint of matrix by determinant. Image Source Below I have shared program to find inverse of 2×2 and 3×3 matrix. Java Program to Find Inverse of a Matrix 2×2 Matrix
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 28 29 30 31 32 33 34 35 |
import java.util.Scanner; public class JavaMatrixInverse { 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"); } } } |
Output Enter elements of matrix row wise: 4 7 2
Read more