import java.util.Scanner;
class MatrixMultiplication {
public static void main(String args[]) {
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();
System.out.println("Enter the number of rows and columns of second matrix");
p = in.nextInt();
q = in.nextInt();
if (n != p)
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else {
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the elements of second matrix");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = in.nextInt();
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k] * second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices:-");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
System.out.print(multiply[c][d] + "\t");
System.out.print("\n");
}
}
}
}
Step-by-Step Explanation:
1. Scanner Class Import:
import java.util.Scanner;
This imports the Scanner class, which the program will use to read user input.
2. Class and method Definition:
class MatrixMultiplication
{
public static void main(String args[])
- The program is defined in the class MatrixMultiplication.
- This is the beginning point for running the program with the method main().
3. Declarations of Variables:
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner(System.in);
- m and n: Row size and column size of the first matrix, or (m rows, n columns).
- p and q: Number of rows and number of columns of the second matrix.
- sum: This variable holds the sum of products while multiplying the matrices.
- c, d, k: Loops used for column and row iterates in unison with dimensions of the matrix.
- Scanner in = new Scanner(System.in); This object will be used for taking input from the user.
4. Obtaining Dimensions and Elements of First Matrix
System.out.println(“Enter the number of rows and columns of first matrix”);
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println(“Enter the elements of first matrix”);
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
first[c][d] = in.nextInt();
- Requests an integer to allow the user to define the number of rows (m) and column of the first matrix (n).
- A two dimensional array 2D of integers that has been declared for the purpose of storing first matrix elements.
- Ask user to read elements of the second matrix using a nested loops, reading one element at a time.
5. Enter the Dimension of the second Matrix
System.out.println(“Enter the number of rows and columns of second matrix”);
p=in.nextInt();
q = in.nextInt();
The above code reads the no of rows and no of column in second matrix
6. Check if is it Valid to multiply Matrix
if(n!= p)
System.out.println(“Matrices with entered orders can’t be multiplied with each other.”);
- The program checks for the possibility of multiplication. To multiply the matrix, the columns of the first must be equal to the number of rows of the second matrix n, p.
- If n!= p then prints an error message and stops.
7. Reading the elements of the Second Matrix
else
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println(“Enter the elements of second matrix”);
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = in.nextInt();
- The program continues if the matrices are multiply compatible.
- The elements of the second matrix will be stored in the newly declared 2D array called second[][]
- The resultant of the matrix multiplication will be stored in another newly declared 2D array called multiply[][].
- Ask the user to input the elements of the second matrix.
8. Matrix Multiplication Logic:
for (c = 0; c < m; c++)
for (d = 0; d < q; d++)
for (k = 0; k < p; k++)
{
sum = sum + first[c][k] * second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
1. Matrix multiplication is accomplished using three nested loops:
- Outer loop (c) iterates the rows of the first matrix.
- Middle loop (d) iterates the columns of the second matrix.
- Innermost loop (k) iterate over the inner dimension, i.e., column of the first matrix and row of the second.
2. Inside the innermost loop multiply the elements of first and second matrices. Add the result to sum.
3. After calculating the summation for the result matrix’s position of the result matrix by multiplying[c][d], it inserts sum into the result matrix and then sets sum to 0 for the next element.
9. Product Matrix Output:
System.out.println(“Product of entered matrices:-“);
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
System.out.print(multiply[c][d] +”\t”);
System.out.print( “”);
}
- It prints the resultant matrix after performing the matrix multiplication, multiply[][].
- The outer c loop shows rows and the inner loop d shows columns.
- Each element of the resultant matrix is printed using tab spacing for a readable purpose.
Example Input/Output:
Sample 1:
Input:
Enter the number of rows and columns of first matrix
2 3
Enter the elements of first matrix
1 2 3
4 5 6
Enter the number of rows and columns of second matrix
3 2
Enter the elements of second matrix
7 8
9 10
11 12
Output:
Product of entered matrices:-
58 64 139 154
Explanation:
First Matrix (2×3):
1 2 3
4 5 6
Second Matrix (3×2):
7 8
9 10
11 12
Multiplying it gives the 2×2 matrix
- The product for multiply[0][0]: 1*7 + 2*9 + 3*11 = 58
- The product for multiply[0][1]: 1*8 + 2*10 + 3*12 = 64
- The product for multiply[1][0]: 4*7 + 5*9 + 6*11 = 139
- For element multiply[1][1]: 4*8 + 5*10 + 6*12 = 154
Write the rows and columns of the first matrix
2 3
3 2
Write the elements of first matrix
1 2 3
4 5 6
Write the rows and columns of the second matrix
2 2
Result
- The matrices with entered orders cannot be multiplied with each other.
- It cannot multiply because, in the example provided, n is not equal to p in these matrices because there are more columns in the first matrix than the number of rows in the second, which is a result of n=3 not equaling p=2.
- Then, the program prints this error message.