-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoDMatrix.java
More file actions
57 lines (47 loc) · 1.89 KB
/
TwoDMatrix.java
File metadata and controls
57 lines (47 loc) · 1.89 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
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
public class TwoDMatrix{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows of first matrix: ");
int rows = sc.nextInt();
System.out.print("\nEnter the number of columns of first matrix: ");
int columns = sc.nextInt();
int[][] arr =new int[rows][columns];
System.out.print("\nEnter the elements of the first array:\n ");
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
arr[i][j]=sc.nextInt();
}
}
System.out.print("Enter the number of rows of second matrix: ");
int rows2 = sc.nextInt();
System.out.print("\nEnter the number of columns of second matrix: ");
int columns2 = sc.nextInt();
int[][] arr2 = new int[rows][columns];
System.out.print("\nEnter the elements of the second array:\n ");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < columns2; j++) {
arr2[i][j] = sc.nextInt();
}
}
System.out.print("\nAddition of the elements of the given Matrices:\n");
if(rows==rows2 && columns==columns2){
int[][] resultMatrix = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
resultMatrix[i][j]=arr[i][j]+arr2[i][j];
}
}
System.out.print("\nAddition of two matrices are:\n ");
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < columns2; j++) {
System.out.print(resultMatrix[i][j] + "\t");
}
System.out.println(" ");
}
}else{
System.out.println("The given matrices cannot be added");
}
sc.close();
}
}