# Java Program to multiply two matrices

```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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 import java.util.Scanner; class matmul { public static void main(String args[]) { int a[][]=new int; int b[][]=new int; int c[][]=new int; System.out.println("Enter the first matrix:"); Scanner input=new Scanner(System.in); for(int i=0;i<3;i++) for(int j=0;j<3;j++) a[i][j]=input.nextInt();   System.out.println("Enter the second matrix:"); for(int i=0;i<3;i++) for(int j=0;j<3;j++) b[i][j]=input.nextInt(); System.out.println("Matrix multiplication is as follows:"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { c[i][j]=0; for(int k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } } for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(a[i][j]+"\t"); } System.out.println("\n"); } System.out.println("\n"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(b[i][j]+"\t"); } System.out.println("\n"); } System.out.println("\n"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(c[i][j]+"\t"); } System.out.println("\n"); } } }```

Output:

Enter the first matrix:
1 2 3 4 5 6 7 8 9

Enter the second matrix:
9 8 7 6 5 4 3 2 1

Matrix multiplication is as follows:
1 2 3
4 5 6
7 8 9

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

9 8 7
6 5 4
3 2 1

30 24 18
84 69 54
138 114 90

### 17 Responses to “Java Program to multiply two matrices”

1. hmmm…..d program is gud but i didn’t knw dat der is a method called scanner….i am an icse student nd i am learnin java at school….i am new to this java environmnt…..anyway its gud…!!!! (^.^)….

2. hello…. d program is quite gud…. and i really hate computer apps…..it is compulsary to learn java in our school ….and ur program was of immense help….i hav mycomputer apps exam 2mrw….!!!! ….thankzz….

3. Faustina

It is very useful for my university exam. Thank you very much… I am studying B.Sc Computer Science. I am very interested in java

4. It is very useful!
thanks for sharing

5. sathish

thanks

6. thanks dude for sharing you see i am an 11 grader of icse and i think i’ll be in need for your further help

7. Sourav Saha

Can i get the algorithms??

8. mahi balan

very good but i cant understand
hence it is very bad
hence proved

9. meow muthu

i only drink milk ,,i cannot understand

10. Dandabani

thats all ah ?????
i expected some wat difficult …
this is simple to my knowledge…
so any doubt means call me… 😛

11. don muthu

i agree with dandabani

12. don muthu

i agree with dandabani

well said dandabani

13. don muthu

ennaku therinja thella kathi,,kuthu

14. don muthu

ennaku therinja thella kathi,,kuthu

15. Dandabani

don muthu … i would lyk to ask abt the cosmopolitan of the analytical differentiation for the fundamental biology of catastrophic formula of dhanush and co. in 3 film ?????

16. The effort is highly appreciative.
The following code is for those who don’t studied scanner classes yet (With algorithms) :

package matrixmultiplication;
import java.io.IOException;

public class MatrixMultiplication {
public static void main(String[] args) throws IOException {

//BufferedReader to read text from character-InputStream and to buferring characters to provide efficient reading of characters

String temp;
System.out.println(“Insert rows in Matrix 1 >> “);
//Reading the user input and copy into temp

//Assigning the value of temp to integer variable M1Row (After parsing string as signed decimal integer)
int M1Row = Integer.parseInt(temp);

System.out.println(“Insert columns in Matrix 1 >> “);
int M1Col = Integer.parseInt(temp);
System.out.println(“Insert rows in Matrix 2 >> “);
int M2Row = Integer.parseInt(temp);
System.out.println(“Insert columns in Matrix 2 >> “);
int M2Col = Integer.parseInt(temp);

//Checking whether the multiplication is applicable
if (M1Col == M2Row) {

//Creating and initializing arrays to hold matrices
int[][] Matrix1 = new int[M1Row][M1Col];
int[][] Matrix2 = new int[M2Row][M2Col];
int[][] Matrix3 = new int[M1Row][M2Col];

//Inserting values in Martix1
System.out.println(“Insert values in Matrix1:”);
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M1Col; j++) {
Matrix1[i][j] = Integer.parseInt(temp);
}
}
//Inserting values in Martix2
System.out.println("Insert values in Matrix2:");
for (int i = 0; i < M2Row; i++) {
for (int j = 0; j < M2Col; j++) {
Matrix2[i][j] = Integer.parseInt(temp);
}
}

//Multiplying two matrices
System.out.println("Multiplying…");
int temp1 = 0;
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M2Col; j++) {
for (int k = 0; k < M2Row; k++) {
temp1 = temp1 + Matrix1[i][k] * Matrix2[k][j];
}
//Inserting elements in result matrix
Matrix3[i][j] = temp1;
temp1 = 0;
}
}

//Display result of Multiplication
System.out.println("Multiplication Completed!");
System.out.println("……………………………………..");
System.out.println("Result");
for (int i = 0; i < M1Row; i++) {
for (int j = 0; j < M2Col; j++) {
System.out.print(Matrix3[i][j] + "\t");
}
System.out.println();
}
System.out.println("……………………………………..");
} else {
System.out.println("Multiplication could not be done!");
System.out.println("Tip: Number of columns in a matrix1 should be equal to number of rows in matrix2 to perform multiplication.");
}
}
}

i hope this helps icse students. Please reply if this helped

##### More from Basic Programs
###### Java program to find whether no. is palindrome or not ### Research Papers ### Projects 