Tuesday , December 6 2016
Home / JAVA / ISC Class 12 / Program for Matrix Multiplication in JAVA

Program for Matrix Multiplication in JAVA

Matrix Multiplication: Here is given a Java Program for Matrix Multiplication also call matrix product. In other words, Let the two matrix to be multiplied be A and B. Let A is an n × m order matrix and B is an m × p order matrix, their matrix product A*B is an n × p order matrix, in which the m entries across the rows of A are multiplied with the m entries down the columns of B.

Steps for Matrix Multiplication in Java:

Step 1: Make sure that the the number of columns in the 1st matrix equals the number of rows in the 2nd matrix.
Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix.
Step 3: Add the products.

Matrix Multiplication in Java

 

Example:

matrix multiplication example

Iteration 1

Multiplication of matrix A’s first row with each element of matrix B’s all columns,

1*1+2*5, 1*2+2*6, 1*3+2*7, 1*4+2*8

Iteration 2

Multiplication of matrix A’s second row with each element of matrix B’s all columns,

3*1+4*5, 3*2+4*6, 3*3+4*7, 3*4+4*8

Iteration 3

Multiplication of matrix A’s third row with each element of matrix B’s all columns,

5*1+6*5, 5*2+6*6, 5*3+6*7, 5*4+6*8

Iteration 4

Add all the multiplication and get the matrix with an order 3*4.

 

  • Output:

    matrix-multiplication-method 1

  •  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

  • Output:

    Enter no. of rows of 1st Matrix : 3
    Enter no. of columns of 1st Matrix : 2
    Enter no. of rows of 2nd Matrix : 2
    Enter no. of columns of 2nd Matrix : 4

    Inputting the 1st Matrix

    Enter an element : 1
    Enter an element : 4
    Enter an element : 8
    Enter an element : 2
    Enter an element : 3
    Enter an element : 6

    Inputting the 2nd Matrix

    Enter an element : 5
    Enter an element : 2
    Enter an element : 3
    Enter an element : 1
    Enter an element : 4
    Enter an element : 5
    Enter an element : 6
    Enter an element : 8

    Output

    The 1st Matrix is
    1 4
    8 2
    3 6

    The 2nd Matrix is
    5 2 3 1
    4 5 6 8

    The Result of Matrix Multiplication is
    21 22 27 33
    48 26 36 24
    39 36 45 51

 

Also See:   Program to check Upper Triangular matrix

Check Also

Program for Fascinating Number in JAVA

Fascinating Number :- The number of 3 digit or more, when we multiplied that number ...

Leave a Reply

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

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.