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[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
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

9 8 7
6 5 4
3 2 1

30 24 18
84 69 54
138 114 90

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

17 thoughts on “Java Program to multiply two matrices

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

  2. 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 ?????

  3. 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.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    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
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

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

    //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 >> “);
    temp = br.readLine();
    int M1Col = Integer.parseInt(temp);
    System.out.println(“Insert rows in Matrix 2 >> “);
    temp = br.readLine();
    int M2Row = Integer.parseInt(temp);
    System.out.println(“Insert columns in Matrix 2 >> “);
    temp = br.readLine();
    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++) {
    temp = br.readLine();
    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++) {
    temp = br.readLine();
    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

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in