forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixAddition.java
More file actions
43 lines (38 loc) · 1.11 KB
/
MatrixAddition.java
File metadata and controls
43 lines (38 loc) · 1.11 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
package arrays;
import java.util.Scanner;
public class MatrixAddition {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Welcome ! to addition program of matrix of same dimensions");
System.out.print("Enter matrix type : ");
int rows = sc.nextInt(), columns = sc.nextInt();
int matrixA[][] = new int [rows][columns];
int matrixB[][] = new int [rows][columns];
System.out.println("Enter matrix A");
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
matrixA[i][j] = sc.nextInt();
}
}
System.out.println("Enter matrix B");
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
matrixB[i][j] = sc.nextInt();
}
}
// Calulation of matrix C
int matrixC[][] = new int [rows][columns];
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
System.out.println("Your matrix C is");
for (int i=0; i<rows; i++) {
for (int j=0; j<columns; j++) {
System.out.print(matrixC[i][j]);
System.out.print(" ");
}System.out.println();
}
}
}