-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrices.java
103 lines (103 loc) · 2.28 KB
/
Matrices.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.*;
class Matrices
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int m1,n1,m2,n2,i,j,k;
char c;
System.out.println("FIRST MATRIX:");
System.out.println("Order: ");
m1=sc.nextInt();
n1=sc.nextInt();
System.out.println("Elements: ");
int a[][]=new int[m1][n1];
for(i=0; i<m1; i++)
{
for(j=0;j<n1;j++)
{
if(j==0)
System.out.println("Enter element into row : "+(i+1));
a[i][j]=sc.nextInt();
}
System.out.println();
}
System.out.println("SECOND MATRIX:");
System.out.println("Order: ");
m2=sc.nextInt();
n2=sc.nextInt();
System.out.println("Elements: ");
int b[][]=new int[m2][n2];
for(i=0; i<m2; i++)
{
for(j=0;j<n2;j++)
{
if(j==0)
System.out.println("Enter element into row : "+(i+1));
b[i][j]=sc.nextInt();
}
System.out.println();
}
System.out.println("Enter the respective sign for the operation to be performed");
System.out.println("+ for addition, - for subtraction, * for multiplication");
c=sc.next().charAt(0);
if(c=='+')
{
if(m1==m2 && n1==n2)
{
System.out.println("Result after ADDITION:");
for(i=0; i<m2; i++)
{
for(j=0;j<n2;j++)
{
System.out.print(a[i][j]+b[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("The order of the matrices are not equal");
}
else if(c=='-')
{
if(m1==m2 && n1==n2)
{
System.out.println("Result after SUBTRACTION:");
for(i=0; i<m2; i++)
{
for(j=0;j<n2;j++)
{
System.out.print(a[i][j]-b[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("The order of the matrices are not equal");
}
else if(c=='*')
{
if(n1==m2)
{
int s[][]=new int[m1][n2];
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
s[i][j]=0;
for(k=0;k<n1;k++)
{
s[i][j]+=a[i][k]*b[k][j];
}
System.out.print(s[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("The order of the matrices are not suitable for multiplication");
}
else
System.out.println("Invalid operation");
}
}