In: Computer Science
To understand the value of counting loops:
Sample Result is shown below:
Enter the number of rows of matrix A: 2
Enter the number of columns of matrix A: 3
Enter the number of columns of matrix B: 3
Enter the number of columns of matrix B: 4
Enter matrix A;
1 2 3
4 5 6
Enter matrix B:
7 8 9 10
11 12 13 14
15 16 17 18
Matrix A:
1 2 3
4 5 6
Matrix B:
7 8 9 10
11 12 13 14
15 16 17 18
Product of matrix A and Matrix B ( A x B) :
74 80 86 92
173 188 203 218
/******************************MatrixMulti.java******************************/
import java.util.Scanner;
class MatrixMulti {
public static void main(String args[]) {
int m, n, p, q, sum = 0, c, d,
k;
Scanner in = new
Scanner(System.in);
System.out.print("Enter the number
of rows of matrix A: ");
m = in.nextInt();
System.out.print("Enter the number
of columns of matrix A: ");
n = in.nextInt();
System.out.print("Enter the
number of columns of matrix B: ");
p = in.nextInt();
System.out.print("Enter the number
of columns of matrix B: ");
q = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter matrix A: ");
for (c = 0; c < m; c++)
for (d = 0; d
< n; d++)
first[c][d] = in.nextInt();
int second[][] = new
int[p][q];
System.out.println("Enter matrix B:
");
for (c = 0; c < p; c++)
for (d = 0; d
< q; d++)
second[c][d] = in.nextInt();
if (n != p)
System.out.println("The matrices can't be multiplied with each
other.");
else {
int multiply[][]
= new int[m][q];
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 matrix A and Matrix B ( A x B) :");
for (c = 0; c
< m; c++) {
for (d = 0; d < q; d++)
System.out.print(multiply[c][d] + "\t");
System.out.print("\n");
}
}
}
}
/***************************************MatrixMulWhile.java***************************/
import java.util.Scanner;
class MatrixMulWhile {
public static void main(String args[]) {
int m, n, p, q, i, j, k, sum;
Scanner in = new
Scanner(System.in);
System.out.print("Enter the
number of rows of matrix A: ");
m = in.nextInt();
System.out.print("Enter the number
of columns of matrix A: ");
n = in.nextInt();
System.out.print("Enter the
number of columns of matrix B: ");
p = in.nextInt();
System.out.print("Enter the number
of columns of matrix B: ");
q = in.nextInt();
if (n == p) {
int mat1[][]
= new int[m][n];
int mat2[][] =
new int[p][q];
int res[][] =
new int[m][q];
System.out.println("Enter the elements of matrix1");
i = 0;
while (i < m)
{
j = 0;
while (j < n)
{
mat1[i][j] =
in.nextInt();
j++;
}
i++;
}
System.out.println("Enter the elements of matrix2");
i = 0;
while (i < p)
{
j = 0;
while (j < q)
{
mat2[i][j] =
in.nextInt();
j++;
}
i++;
}
System.out.println("Product of matrix A and Matrix B ( A x B)
:");
i = 0;
while (i < m)
{
j = 0;
while (j < q) {
sum = 0;
k = 0;
while (k < p) {
sum +=
mat1[i][k] * mat2[k][j];
k++;
}
res[i][j] = sum;
j++;
}
i++;
}
i = 0;
while (i < m)
{
j = 0;
while (j < q) {
System.out.print(res[i][j] +
" ");
j++;
}
System.out.println();
i++;
}
} else
System.out.println("The matrices can't be multiplied with each
other.");
}
}
/*********************output**********************/
Enter the number of rows of matrix A: 2
Enter the number of columns of matrix A: 3
Enter the number of columns of matrix B: 3
Enter the number of columns of matrix B: 4
Enter the elements of matrix1
1 2 3
4 5 6
Enter the elements of matrix2
7 8 9 10
11 12 13 14
15 16 17 18
Product of matrix A and Matrix B ( A x B) :
74 80 86 92
173 188 203 218
Please let me know if you have any doubt or modify the answer, Thanks :)