forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrixAddition.java
50 lines (41 loc) · 1.4 KB
/
MatrixAddition.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
//WHAT IS MATRIX ADDITION ?
//In mathematics, matrix addition is the operation of adding two matrices by adding the corresponding entries together.
//However, there are other operations which could also be considered addition for matrices, such as the direct sum and the Kronecker sum.
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();
}
}
}