# How To Implement Matrix Multiplication In Java?

Last updated on Jun 17,2021 7.9K Views 27 / 29 Blog from Java Programs

## Matrix Multiplication In Java

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 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

## Specify Input Through Keyboard

```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 Certification Course 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.7142914,"math_prob":0.98854196,"size":3102,"snap":"2021-31-2021-39","text_gpt3_token_len":913,"char_repetition_ratio":0.1307295,"word_repetition_ratio":0.14760147,"special_character_ratio":0.34880722,"punctuation_ratio":0.17086835,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9946831,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-21T02:52:13Z\",\"WARC-Record-ID\":\"<urn:uuid:ba6a6e85-6828-46c5-accb-5ffdca6039b8>\",\"Content-Length\":\"212587\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:039a8274-f338-4b93-bfbd-dee95cce125c>\",\"WARC-Concurrent-To\":\"<urn:uuid:230f5d75-acd3-4ed0-933e-6ffeed160889>\",\"WARC-IP-Address\":\"52.85.61.118\",\"WARC-Target-URI\":\"https://www.edureka.co/blog/matrix-multiplication-in-java/\",\"WARC-Payload-Digest\":\"sha1:VHYBOESMQVKDRIQCYJ3BKRVZ7BFZE6ZN\",\"WARC-Block-Digest\":\"sha1:5OSGN66VT4KFXGX57TBWYVSF7IKTCSVS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057131.88_warc_CC-MAIN-20210921011047-20210921041047-00144.warc.gz\"}"}