-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTwoDMatrixSum.java
68 lines (60 loc) · 1.21 KB
/
TwoDMatrixSum.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
import java.util.Random;
public class TwoDMatrixSum{
public static void main(String ar[]){
int[][] a= new int[3][3];
int[][] b= new int[3][3];
int[][] c= new int[3][3];
Random r1= new Random();
Random r2= new Random();
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
a[i][j]=r1.nextInt(100);
}
}
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
b[i][j]=r2.nextInt(100);
}
}
System.out.println("1st Matrix is as follows :");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println("");
}
System.out.println("2nd Matrix is as follows :");
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println("");
}
System.out.println("");
for(int i=0;i<a.length;i++)
{
for(int j=0;j<a[i].length;j++)
{
c[i][j]=0;
c[i][j]=c[i][j]+a[i][j]+b[i][j];
}
}
System.out.println("Sum of two Matrix are as follows :");
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
}