-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixAdd3D.java
49 lines (49 loc) · 951 Bytes
/
MatrixAdd3D.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
/**MatrixAdd3D*/
import java.util.Scanner;
class MatrixAdd3D
{
public static void main(String[] args)
{
int rows,cols,i,j,k;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the total no. of rows & columns of the Matrix:");
rows=sc.nextInt();
cols=sc.nextInt();
int a[][][]=new int[3][rows][cols];
//input
for(k=0;k<2;k++)
{
System.out.println("Input Matrix "+(k+1)+":");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
a[k][i][j]=sc.nextInt();
}
}
//System.out.println();
}
//process
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
a[2][i][j]=a[0][i][j]+a[1][i][j];
}
}
//Output
for(k=0;k<3;k++)
{
System.out.println("Matrix "+(k+1)+":");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
System.out.printf("%3d ",a[k][i][j]);
}
System.out.println();
}
System.out.println();
}
}
}