Matrix Multiplication Program in Java
import java.util.*;
import java.util.*;
public class MatrixMultiplication
{
public static void main(String[] args)
{
int ar1[][] = new int[3][3];
int ar2[][] = new int[3][3];
int mul[][] = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter first matrix:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print("Enter the number ar1[" + i + "][" + j + "]:");
ar1[i][j] = sc.nextInt();
}
}
System.out.println("Enter second matrix:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print("Enter the number ar2[" + i + "][" + j + "]:");
ar2[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mul[i][j] = 0;
for (int k = 0; k < 3; k++)
{
mul[i][j] = mul[i][j] + (ar1[i][k] * ar2[k][j]);
}
}
}
System.out.println("Multiplication of Matrices:");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
System.out.print(mul[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Enter first matrix: Enter the number ar1[0][0]:2 Enter the number ar1[0][1]:2 Enter the number ar1[0][2]:2 Enter the number ar1[1][0]:2 Enter the number ar1[1][1]:2 Enter the number ar1[1][2]:2 Enter the number ar1[2][0]:2 Enter the number ar1[2][1]:2 Enter the number ar1[2][2]:2 Enter second matrix: Enter the number ar2[0][0]:3 Enter the number ar2[0][1]:3 Enter the number ar2[0][2]:3 Enter the number ar2[1][0]:3 Enter the number ar2[1][1]:3 Enter the number ar2[1][2]:3 Enter the number ar2[2][0]:3 Enter the number ar2[2][1]:3 Enter the number ar2[2][2]:3 Multiplication of Matrices: 18 18 18 18 18 18 18 18 18java tutorials learn java study java