## Java, J2EE & SOA Certification Training

- 34k Enrolled Learners
- Weekend
- Live Class

This article will introduce you to a very common problem that if dealt with, eases many tasks. This article will discuss Matrix Multiplication In Java. Following pointers will be discussed in this article,

So let us get started with this article,

Obtaining a single matrix from the entries of two matrices by using a binary operation is known as Matrix multiplication. In simpler terms, if two matrices R and S of order a*b and b*c are multiplied, the matrix obtained is of the order a*c. Multiplication of a matrix can be done efficiently in java by using various methods. The most effective method is discussed below.

**Using For Loop**

In this method, we make usage of for loop.

public class Main{ public static void main(String args[]){ //creating two matrices int m1[][]={{1,2,3},{4,5,6},{2,3,4}}; int m2[][]={{1,2,3},{4,5,6},{2,3,4}}; int m[][]=new int[3][3]; //3 rows and 3 columns //multiplying for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ m[i][j]=0; for(int k=0;k<3;k++) { m[i][j]+=m1[i][k]*m2[k][j]; } //end of k loop System.out.print(m[i][j]+" "); //printing matrix } //end of j loop System.out.println(); } }}

**Output**

15 21 27

36 51 66

22 31 40

Moving on with this article on Matrix Multiplication In Java,

import java.util.Scanner; public class Main { public static void main(String args[]) { int n; Scanner input = new Scanner(System.in); System.out.println("Enter base of matrices"); n = input.nextInt(); int[][] m1 = new int[n][n]; int[][] m2 = new int[n][n]; int[][] mat = new int[n][n]; System.out.println("Enter the elements of 1st matrix row wise : n"); for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { m1[i][j] = input.nextInt(); } } System.out.println("Enter the elements of 2nd matrix row wise : n"); for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { m2[i][j] = input.nextInt(); } } System.out.println("Multiplying the matrices : "); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { mat[i][j] = mat[i][j] + m1[i][k] * m2[k][j]; } } } System.out.println("Product :"); for (int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { System.out.print(mat[i][j] + " "); } System.out.println(); } input.close(); } }

**Output**

**Enter base of matrices:**

3

**Enter the elements of 1st matrix row wise:**

1

2

3

6

5

4

7

8

9

**Enter the elements of 2nd matrix row wise :**

3

2

1

4

5

6

9

8

7

**Multiplying the matrices:**

**Product:**

38 36 34

270 314 358

134 126 118

*Thus, the product of two matrices can be found efficiently by using the for loop in java.*

Thus we have come to an end of this article on ‘Matrix Multiplication in Java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

*Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.*

REGISTER FOR FREE WEBINAR

Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP