-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathMatrix.java
More file actions
50 lines (47 loc) · 1.27 KB
/
Matrix.java
File metadata and controls
50 lines (47 loc) · 1.27 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
import java.util.*;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size: ");
int n =sc.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
System.out.println("Enter elements in A: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements in B: ");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = sc.nextInt();
}
}
sc.close();
int[][] result = new int[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}
System.out.println("Matrix after addition:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(result[i][j]+" ");
}
System.out.println("");
}
}
}