public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
//creating another matrix to store the multiplication of two matrices
int c[][]=new int[3][3]; //3 rows and 3 columns
//multiplying and printing multiplication of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
System.out.print(c[i][j]+" "); //printing matrix element
}//end of j loop
System.out.println();//new line
}
}}
// Java program to multiply two square
// matrices.
import java.io.*;
class GFG {
static int N = 4;
// This function multiplies mat1[][]
// and mat2[][], and stores the result
// in res[][]
static void multiply(int mat1[][],
int mat2[][], int res[][])
{
int i, j, k;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
res[i][j] = 0;
for (k = 0; k < N; k++)
res[i][j] += mat1[i][k]
* mat2[k][j];
}
}
}
// Driver code
public static void main(String[] args)
{
int mat1[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int mat2[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// To store result
int res[][] = new int[N][N];
int i, j;
multiply(mat1, mat2, res);
System.out.println("Result matrix"
+ " is ");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
System.out.print(res[i][j]
+ " ");
System.out.println();
}
}
}
// This code is contributed by anuj_67.