forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add2Matrix.java
48 lines (37 loc) · 1 KB
/
Add2Matrix.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
package introduction;
import java.util.Scanner;
public class Add2Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the dimensions");
int row = sc.nextInt();
int col = sc.nextInt();
int[][] a = new int[row][col];
int[][] b = new int[row][col];
int[][] c = new int[row][col];
System.out.println("Enter the elements of a :");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of b :");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
b[i][j] = sc.nextInt();
}
}
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The matrix c is ");
for(int i = 0; i < row; i++) {
for(int j = 0; j < col; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}