-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.java
77 lines (77 loc) · 2.57 KB
/
matrix.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
import java.util.Scanner;
public class matrix {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter The No. Of Rows(m) Of The 1st Matrix:");
int m1=sc.nextInt();
System.out.println("Enter The No. Of Columns(n) Of The 1st Matrix:");
int n1=sc.nextInt();
System.out.println("Now Enter The Elements In The 1st Matrix Row-wise:");
int [][] arr1=new int[m1][n1];
for(int i=0; i<m1; i++){
for(int j=0; j<n1; j++){
arr1[i][j]=sc.nextInt();
}
}
System.out.println("Enter The No. Of Rows(m) Of The 2nd Matrix:");
int m2=sc.nextInt();
System.out.println("Enter The No. Of Columns(n) Of The 2nd Matrix:");
int n2=sc.nextInt();
System.out.println("Now Enter The Elements In The 2nd Matrix Row-wise:");
int [][] arr2=new int[m2][n2];
for(int i=0; i<m2; i++){
for(int j=0; j<n2; j++){
arr2[i][j]=sc.nextInt();
}
}
System.out.println("Which Operation Do You Want To Perform?\n1.Addition\n2.Subtraction\n3.Multiplication");
int op=sc.nextInt();
int [][] arr=new int[m1][n1];
switch(op){
case 1:
if(m1==m2 && n1==n2){
for(int i=0; i<m1; i++){
for(int j=0; j<n1; j++){
arr[i][j]=arr1[i][j]+arr2[i][j];
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
else{
System.out.println("Summation Of The Given 2 Matrices Is Not Possible");
}
break;
case 2:
if(m1==m2 && n1==n2){
for(int i=0; i<m1; i++){
for(int j=0; j<n1; j++){
arr[i][j]=arr1[i][j]-arr2[i][j];
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
else{
System.out.println("Subtraction Of The Given 2 Matrices Is Not Possible");
}
break;
case 3:
if(m1==n2){
int [][] arrm=new int[m1][n2];
for(int i=0;i<m1;i++){
for(int j=0;j<n2;j++){
for(int k=0;k<n1;k++){
arrm[i][j]=arrm[i][j] + arr1[i][k]*arr2[k][j];
}
System.out.print(arrm[i][j]+" ");
}
System.out.println();
}
}
else{
System.out.println("Multiplication Of The Given 2 Matrices Is Not Possible");
}
}
}
}