forked from VimalKrishnaRao/OOP-in-Java-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultmat.java
42 lines (42 loc) · 1.36 KB
/
Multmat.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
import java.util.Scanner;
class Multmat
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
System.out.println ("Enter the Order of 1st Matrix: ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println ("Enter the Order of 2nd Matrix: ");
int c = sc.nextInt();
int d = sc.nextInt();
int m1[][]=new int [a][b];
int m2[][]=new int [c][d];
System.out.println ("Enter 1st Matrix:");
for (int i=0; i<a; i++)
for (int j=0; j<b; j++)
m1[i][j]=sc.nextInt();
System.out.println ("Enter 2nd Matrix:");
for (int i=0; i<c; i++)
for (int j=0; j<d; j++)
m2[i][j]=sc.nextInt();
if (b==c)
{
int m3[][]=new int[a][d];
System.out.println ("The Final Product Matrix is: ");
for (int i=0; i<a; i++)
{
for (int j=0; j<d; j++)
{
m3[i][j]=0;
for (int k=0; k<b; k++)
m3[i][j]+=m1[i][k]*m2[k][j];
System.out.print (m3[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println ("Give matrices cannot be multiplied");
}
}