-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddMatrix.java
More file actions
58 lines (53 loc) · 1.19 KB
/
addMatrix.java
File metadata and controls
58 lines (53 loc) · 1.19 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
51
52
53
54
55
56
57
58
import java.util.Scanner;
class addMatrix
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of the matrix\n");
int r = sc.nextInt();
int c = sc.nextInt();
int arr1[][] = new int[r][c];
int arr2[][] = new int[r][c];
int sum[][] = new int[r][c];
int i,j;
System.out.println("Enter the elements of first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
arr1[i][j] = sc.nextInt();
}
System.out.println("Enter the elements of second matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
arr2[i][j] = sc.nextInt();
}
System.out.println("first matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(arr1[i][j]+" ");
System.out.println();
}
System.out.println("second matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(arr2[i][j]+" ");
System.out.println();
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
sum[i][j] = arr1[i][j] + arr2[i][j];
}
System.out.println(" sum of two matrices");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.print(sum[i][j]+" ");
System.out.println();
}
}
}