import java.io.*;
public class MatrixMultiplication
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][], c[][];
// Read Number of Rows and Columns of First Matrix
System.out.print("Enter Number of Rows of 1st matrix:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns of 1st matrix:: ");
int n = Integer.parseInt(br.readLine());
int i, j, k;
a = new int[m][n];
// Read First Matrix Elements From User
System.out.println("\nEnter First Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Read Number of Columns of Second Matrix
System.out.print("\nEnter Number of Columns of 2nd matrix:: ");
int p = Integer.parseInt(br.readLine());
// Note: Only Column of Second Matrix is Taken
// Because the Row of Second Matrix
// should be Equal to Column of First Matrix
b = new int[n][p];
// Read Second Matrix Elements From User
System.out.println("\nEnter Second Matrix Data::");
for (i = 0; i < n; i++)
{
for (j = 0; j < p; j++)
{
System.out.print("Enter Element:: ");
b[i][j] = Integer.parseInt(br.readLine());
}
}
// Resultant Matrix has Rows equal to that of First Matrix
// and Columns equal to that of Second Matrix
c = new int[m][p];
// Perform Matrix Multiplication
for (i = 0; i < m; i++)
for (j = 0; j < p; j++)
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
// Display Result Matrix
System.out.println("\nResultant Matrix::");
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
{
System.out.print("\t" + c[i][j]);
}
System.out.println();
}
}
}
public class MatrixMultiplication
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][], b[][], c[][];
// Read Number of Rows and Columns of First Matrix
System.out.print("Enter Number of Rows of 1st matrix:: ");
int m = Integer.parseInt(br.readLine());
System.out.print("Enter Number of Columns of 1st matrix:: ");
int n = Integer.parseInt(br.readLine());
int i, j, k;
a = new int[m][n];
// Read First Matrix Elements From User
System.out.println("\nEnter First Matrix Data::");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Read Number of Columns of Second Matrix
System.out.print("\nEnter Number of Columns of 2nd matrix:: ");
int p = Integer.parseInt(br.readLine());
// Note: Only Column of Second Matrix is Taken
// Because the Row of Second Matrix
// should be Equal to Column of First Matrix
b = new int[n][p];
// Read Second Matrix Elements From User
System.out.println("\nEnter Second Matrix Data::");
for (i = 0; i < n; i++)
{
for (j = 0; j < p; j++)
{
System.out.print("Enter Element:: ");
b[i][j] = Integer.parseInt(br.readLine());
}
}
// Resultant Matrix has Rows equal to that of First Matrix
// and Columns equal to that of Second Matrix
c = new int[m][p];
// Perform Matrix Multiplication
for (i = 0; i < m; i++)
for (j = 0; j < p; j++)
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
// Display Result Matrix
System.out.println("\nResultant Matrix::");
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
{
System.out.print("\t" + c[i][j]);
}
System.out.println();
}
}
}
OUTPUT::
Enter Number of Rows of 1st matrix:: 2
Enter Number of Columns of 1st matrix:: 3
Enter First Matrix Data::
Enter Element:: 1
Enter Element:: 2
Enter Element:: 3
Enter Element:: 4
Enter Element:: 5
Enter Element:: 6
Enter Number of Columns of 2nd matrix:: 2
Enter Second Matrix Data::
Enter Element:: 7
Enter Element:: 8
Enter Element:: 9
Enter Element:: 10
Enter Element:: 11
Enter Element:: 12
Resultant Matrix::
58 64
139 154