forked from MLSA-MUET-SZAB-Club-Khairpur-Mir-s/Learn-to-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddtwomatrices.java
43 lines (33 loc) · 1.18 KB
/
Addtwomatrices.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
import java.util.Scanner;
public class Addtwomatrices {
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println("Input number of rows of matrix");
m = in.nextInt();
System.out.println("Input number of columns of matrix");
n = in.nextInt();
int array1[][] = new int[m][n];
int array2[][] = new int[m][n];
int sum[][] = new int[m][n];
System.out.println("Input elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
array1[c][d] = in.nextInt();
System.out.println("Input the elements of second matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
array2[c][d] = in.nextInt();
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = array1[c][d] + array2[c][d];
System.out.println("Sum of the matrices:-");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}