-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddMatrix.java
More file actions
46 lines (37 loc) · 1.06 KB
/
Copy pathAddMatrix.java
File metadata and controls
46 lines (37 loc) · 1.06 KB
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
//Simple java code for addition of matrix.
import java.util.*;
public class AddMatrix {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows = in.nextInt();
System.out.println("Enter the number of columns");
int columns = in.nextInt();
int [][] matrix = new int[rows][columns];
int [][] matrix2 = new int[rows][columns];
System.out.println("Enter the first Matrix"); //1st Matrix
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
matrix[i][j] = in.nextInt();
}
}
System.out.println("Enter the second Matrix"); //2nd Matrix
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
matrix2[i][j] = in.nextInt();
}
}
int [][] matrix3 = new int[rows][columns];
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
matrix3[i][j] = matrix[i][j]+matrix2[i][j]; //Addition of Matrix
}
}
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
System.out.print(matrix3[i][j]);
}
}
}
}