-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrixaddition.java
59 lines (59 loc) · 1.48 KB
/
Matrixaddition.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
import java.util.Scanner;
public class Matrixaddition
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int r,c, m, n;
int ar[][]= new int[20][20];
int br[][]= new int[20][20];
System.out.println("NOTE: To add two matrices they should be of same order.");
System.out.println("Enter the no.of rows of matrix1: ");
r = sc.nextInt();
System.out.println("Enter the number of columns of matrix2: ");
c = sc.nextInt();
System.out.println("Enter the no.of rows of matrix2: ");
m = sc.nextInt();
System.out.println("Enter the no.of columns of matrix2: ");
n = sc.nextInt() ;
if(r==m&&n==c)
{
System.out.println("Enter the elements of first matrix.");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
ar[i][j]=sc.nextInt();
}
}
System.out.println("Enter elements of second matrix.");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
br[i][j]=sc.nextInt();
}
}
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
ar[i][j]+=br[i][j];
}
}
System.out.println("Sum of matrices: ");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(" "+ar[i][j]);
}
System.out.println("\n");
}
}
else
{
System.out.println("Addition not possible.");
}
}
}