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

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…!!!! (^.^)….

    Reply
  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….

    Reply
  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

    Reply
  4. 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

    Reply
  5. mahi balan

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

    Reply
  6. Dandabani

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

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

    Reply
  8. 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

    Reply

Leave a Reply