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.

Java Program to Find Inverse of a Matrix

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

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 6

determinant = 10.0

Inverse of matrix is:
0.6 -0.7
-0.2 0.4

3×3 Matrix

import java.util.Scanner;

public class JavaMatrixInverse {
	public static void main(String args[]) {
		int i, j;
		float det = 0;
		float mat[][] = new float[3][3];
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter elements of matrix row wise:");
		for(i = 0; i < 3; ++i)
			for(j = 0; j < 3; ++j)
				mat[i][j] = sc.nextFloat();
		
	    for(i = 0; i < 3; i++)
	        det = det + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));
		
		System.out.println("\ndeterminant = " + det);
				
		System.out.println("\nInverse of matrix is:");
		for(i = 0; i < 3; ++i) {
			for(j = 0; j < 3; ++j)
				System.out.print((((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ det) + " ");
			
			System.out.print("\n");
		}
	}
}

Output

Enter elements of matrix row wise:
1 2 3
0 1 4
5 6 0

determinant = 1.0

Inverse of matrix is:
-24.0 18.0 5.0
20.0 -15.0 -4.0
-5.0 4.0 1.0

Comment below if you have any queries related to above program to find inverse of matrix in java.

3 thoughts on “Java Program to Find Inverse of a Matrix”

    1. Hmmmmm.

      I may try to make an inverse. Check the loop – it has %3 (modulo 3) many places. And it has loops going up to and not including 3. This may be a part of it. Furthermore we may need to see how matrix multiplication is done with 3, 4 and 5 dimensions – then that may give a clue..

      One day … Hmmmm

  1. Thank you so much for providing such software snippets. It is a great help for us who are maybe not THAAAAAT super experts in mathematics.

    If you on the other hand, one day, need assistance on algorithms, feel welcome.

Leave a Comment

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