-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_matrices.java
45 lines (43 loc) · 1.02 KB
/
add_matrices.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
//write a java program to add two matrix
import java.util.Scanner;
public class add_matrices {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int c[][]=new int[2][2];
int i,j;
System.out.println("enter first matrix: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("enter second matrix: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
b[i][j]=sc.nextInt();
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
System.out.println("resultant matrix: ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.println(c[i][j]+ " ");
}
}
}
}