-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatMultiplication.txt
64 lines (50 loc) · 1.71 KB
/
MatMultiplication.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
pulic class MatMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, i, j, k;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = sc.nextInt();
n = sc.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
first[i][j] = sc.nextInt();
System.out.println("Enter the number of rows and columns of second matrix");
p = sc.nextInt();
q = sc.nextInt();
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
System.out.println("Enter the elements of second matrix");
for ( i = 0 ; i < p ; i++ )
for ( j = 0 ; j < q ; j++ )
second[i][j] = sc.nextInt();
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < q ; j++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[i][k]*second[k][j];
}
multiply[i][j] = sum;
sum = 0;
}
}
System.out.println("Product of entered matrices: ");
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < q ; j++ )
System.out.print(multiply[i][j]+" ");
System.out.print("\n");
}
}
}
}