Matrix Multiplication Program in Java

Matrix Multiplication Program in Java
Matrix Multiplication Program in Java
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);
        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();
            }
        }
        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++)
            {
                for (int k = 0; k < 3; k++)
                {
                    mul[i][j] = ar1[i][k] + ar2[k][j];
                }
            }
        }
        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]:1
Enter the number ar1[0][1]:2
Enter the number ar1[0][2]:3
Enter the number ar1[1][0]:4
Enter the number ar1[1][1]:5
Enter the number ar1[1][2]:6
Enter the number ar1[2][0]:7
Enter the number ar1[2][1]:8
Enter the number ar1[2][2]:9
Enter second matrix:
Enter the number ar2[0][0]:2
Enter the number ar2[0][1]:2
Enter the number ar2[0][2]:2
Enter the number ar2[1][0]:2
Enter the number ar2[1][1]:2
Enter the number ar2[1][2]:2
Enter the number ar2[2][0]:2
Enter the number ar2[2][1]:2
Enter the number ar2[2][2]:2
Multiplication of Metrices:
5 5 5 
8 8 8 
11 11 11
java tutorials learn java study java
Recent Posts