-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathMatrix mul.java
54 lines (52 loc) · 1.04 KB
/
Matrix mul.java
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
/*Matrix A--mxn Matrix B--pxq,
if n==p then we can perform matrix multliplication
Resultant Matrix C--mxq */
import java.util.*;
class MatrixMulLe11
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of rows and columns of A:");
int m=sc.nextInt();
int n=sc.nextInt();
System.out.println("Enter no of rows and columns of b:");
int p=sc.nextInt();
int q=sc.nextInt(),i,j,k;
if(n!=p)
{
System.out.println("Multiplication not possible..!!");
}
else
{
int[][] a=new int[m][n];
int[][] b=new int[p][q];
int[][] c=new int[m][q];
System.out.println("Enter elements of matrix A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
System.out.println("Enter elements of matrix B:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[i][j]=sc.nextInt();
//Matrix multiplication
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
System.out.println("Resultant Matric C:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
System.out.print(c[i][j]+" ");
System.out.println(" ");
}
}
}
}