Java Program to Perform Matrix Multiplication
In this tutorial, we will learn how to perform matrix multiplication. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.
Below is the pictorial representation for the same.
Input:
Enter the number of rows in the first matrix: 3
Enter the number of columns in the first matrix: 3
Enter the number of rows in the second matrix: 3
Enter the number of rows in the second matrix: 3
Enter all the elements of the first matrix: 1 2 3 4 5 6 7 8 9
Enter all the elements of the second matrix: 9 8 7 6 5 4 3 2 1
Output:
First Matrix:
1 2 3
4 5 6
7 8 9
Second Matrix:
9 8 7
6 5 4
3 2 1
Resultant Matrix:
30 24 18
84 69 54
138 114 90
Program 1: Perform Matrix Multiplication
In this program, we will perform matrix multiplication. But for matrix multiplication to take place, the number of columns of the first matrix must be equal to the number of rows of the second matrix.
Algorithm
- Start
- Declare variables for matrix size.
- Initialize the number of rows and columns for the first matrix.
- Initialize the number of rows and columns for the second matrix.
- Declare two matrices.
- Ask the user to initialize the matrices.
- Call a method to multiply the two matrices.
- Print the two matrices.
- Check if matrix multiplication is possible or not.
- If possible, then create a new Matrix to store the product of the two matrices.
- Traverse each element of the two matrices and multiply them.
- Store this product in the new matrix at the corresponding index.
- Print the final product matrix.
- If matrix multiplication is not possible, then display the same.
- Stop.
Below is the code for the same in Java language.
/*Java Program to multiply two matrices*/
import java.util.Scanner;
public class Main
{
// To print Matrix
static void printMatrix(int M[][], int rowSize, int colSize)
{
for (int i = 0; i < rowSize; i++)
{
for (int j = 0; j < colSize; j++)
{
System.out.print(M[i][j] + " ");
}
System.out.println();
}
}
// To multiply two matrices a[][] and b[][]
static void multiplyMatrix(int p,int q, int a[][], int m, int n, int b[][])
{
int i, j, k;
// Print the matrices A and B
System.out.println("First Matrix:");
printMatrix(a, p, q);
System.out.println("Second Matrix:");
printMatrix(b, m, n);
// Check if multiplication is Possible
if (m != q)
{
System.out.println("Multiplication Not Possible");
return;
}
// Matrix to store the result
int c[][] = new int[q][n];
// Multiply the two matrices
for (i = 0; i < p; i++)
{
for (j = 0; j < n; j++)
{
for (k = 0; k < m; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
// Print the result
System.out.println("\nResultant Matrix:");
printMatrix(c, p, n);
}
//Driver Code
public static void main(String[] args)
{
int p, q, m, n; //Declare matrix size
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in the first matrix:");
p = sc.nextInt(); //Initialize first matrix size
System.out.print("Enter the number of columns in the first matrix:");
q = sc.nextInt(); //Initialize first matrix size
System.out.print("Enter the number of rows in the second matrix:");
m = sc.nextInt(); //Initialize second matrix size
System.out.print("Enter the number of columns in the second matrix:");
n = sc.nextInt(); //Initialize second matrix size
int a[][] = new int[p][q]; //Declare first matrix
int b[][] = new int[m][n]; //Declare second matrix
//Initialize the first Matrix
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("");
//Initialize the second matrix
System.out.println("Enter all the elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
}
//To Multiply two matrices
multiplyMatrix(p ,q, a, m, n, b);
}
}
Enter the number of rows in the first matrix:3
Enter the number of columns in the first matrix:3
Enter the number of rows in the second matrix:3
Enter number of columns in the second matrix:3
Enter all the elements of first matrix: 1 2 3 4 5 6 5 4 3
Enter all the elements of the second matrix: 6 5 4 7 1 2 3 4 5
First Matrix:
1 2 3
4 5 6
5 4 3
Second Matrix:
6 5 4
7 1 2
3 4 5
Resultant Matrix:
29 19 23
77 49 56
67 41 43
Program 2: Perform Matrix Multiplication
In this program, we will perform matrix multiplication. Matrix multiplication is a simple binary operation that produces a single matrix from the two given matrices. When two matrices of order m*n and n*p are multiplied, the resultant matrix will be of the order m*p.
Algorithm
- Start
- Declare variables for matrix size.
- Initialize the number of rows and columns for the first matrix.
- Initialize the number of rows and columns for the second matrix.
- Declare two matrices.
- Ask the user to initialize the matrices.
- Print the two matrices.
- Check if matrix multiplication is possible or not.
- If possible, then create a new Matrix to store the product of the two matrices.
- Traverse each element of the two matrices and multiply them.
- Store this product in the new matrix at the corresponding index.
- Print the final product matrix.
- If matrix multiplication is not possible, then display the same.
- Stop.
Below is the code for the same in Java language.
/*Java Program to multiply two matrices*/
import java.util.Scanner;
public class Main
{
//Driver Code
public static void main(String[] args)
{
//Take input from user
Scanner sc = new Scanner(System.in);
int p, q, m, n; //Declare matrix size
System.out.print("Enter the number of rows in the first matrix:");
p = sc.nextInt(); //Initialize the the first matrix size
System.out.print("Enter number of columns in the first matrix:");
q = sc.nextInt(); //Initialize first matrix size
System.out.print("Enter the number of rows in the second matrix:");
m = sc.nextInt(); //Initialize second matrix size
System.out.print("Enter the number of columns in the second matrix:");
n = sc.nextInt(); //Initialize second matrix size
int a[][] = new int[p][q]; //Declare first matrix
int b[][] = new int[m][n]; //Declare second matrix
//Initialize the first Matrix
System.out.println("Enter all the elements of first matrix:");
for (int i = 0; i < p; i++)
{
for (int j = 0; j < q; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("");
//Initialize the second matrix
System.out.println("Enter all the elements of second matrix:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
}
//Print the First Matrix
System.out.println("First Matrix:");
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
//Print Second Matrix
System.out.println("Second Matrix:");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
// Check if multiplication is Possible
if (m != q) {
System.out.println("Multiplication Not Possible");
return;
}
// Matrix to store the result
int c[][] = new int[q][n];
int k=0;
// Multiply the two matrices
for(int i=0;i<p;i++)
{
for(int j=0;j<n;j++)
{
for (k = 0; k < m; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
// Print the resultant matrix
System.out.println("Resultant Matrix:");
for(int i=0;i<q;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}
Enter the number of rows in the first matrix:3
Enter the number of columns in the first matrix:3
Enter the number of rows in the second matrix:3
Enter the number of columns in the second matrix: 3
Enter all the elements of the first matrix:1 2 3 4 5 6 7 8 9
Enter all the elements of the second matrix: 3 4 5 2 6 7 1 2 1
First Matrix:
1 2 3
4 5 6
7 8 9
Second Matrix:
3 4 5
2 6 7
1 2 1
Resultant Matrix:
10 22 22
28 58 61
46 94 100