-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Matrix_addition.java
77 lines (71 loc) · 2.31 KB
/
Matrix_addition.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
/*
In mathematics, matrix addition is the operation of adding two matrices by adding
the corresponding entries together.This program takes two matrices of order n*m and
stores it in two-dimensional array. Then, the program adds these two matrices and
displays it on the screen.
*/
import java.util.Scanner;
class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows of the first matrix");
int n1 = sc.nextInt();
System.out.println("Enter the number of columns of the first matrix");
int m1 = sc.nextInt();
System.out.println("Enter the elements of the first matrix");
int arr_1[][] = new int[n1][m1];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
arr_1[i][j] = sc.nextInt();
}
}
System.out.println("Enter the number of rows of the second matrix");
int n2 = sc.nextInt();
System.out.println("Enter the number of columns of the second matrix");
int m2 = sc.nextInt();
System.out.println("Enter the elements of the second matrix");
int arr_2[][] = new int[n2][m2];
for (int i = 0; i < n2; i++) {
for (int j = 0; j < m2; j++) {
arr_2[i][j] = sc.nextInt();
}
}
int res[][] = new int[n1][m1];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
res[i][j] = arr_1[i][j] + arr_2[i][j];
}
}
System.out.println("The result of matrix addition is :");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
System.out.print(res[i][j] + " ");
}
System.out.println();
}
sc.close();
}
}
/*
Sample I/O:
Enter the number of rows of the first matrix 3
Enter the number of columns of the first matrix 3
Enter the elements of the first matrix
1 2 3
4 5 6
7 8 9
Enter the number of rows of the second matrix 3
Enter the number of columns of the second matrix 3
Enter the elements of the second matrix
9 8 7
6 5 4
3 2 1
The result of matrix addition is :
10 10 10
10 10 10
10 10 10
Time complexity = O(n*m)
Space complexity = O(n*m)
n : number of rows
M : number of columns
*/