-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAddTwoMatrixInArray.java
More file actions
56 lines (56 loc) · 1.48 KB
/
AddTwoMatrixInArray.java
File metadata and controls
56 lines (56 loc) · 1.48 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
//WAP in java define a method to add two matrix int Array.
import java.util.Scanner;
public class AddTwoMatrixInArray{
public static void main(String[] args) {
int x[][] = readMatrix();
int y[][] = readMatrix();
int z[][] = addTwoMatrixInArray(x,y);
displayMatrix(z);
}
public static int[][] readMatrix(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows you want : ");
int r = sc.nextInt();
System.out.println("Enter the number of column you want : ");
int c = sc.nextInt();
int mat[][] = new int[r][c];
System.out.println("Enter "+r*c+" elements row wise : ");
for(int i=0;i<mat.length;i++)
{
for(int j=0;j<mat[i].length;j++)
{
mat[i][j]=sc.nextInt();
}
}
return mat;
}
public static void displayMatrix(int z[][])
{
System.out.println("The Addition of two matrix is : ");
for(int i=0;i<z.length;i++)
{
for(int j=0;j<z[i].length;j++)
{
System.out.print(z[i][j]+" ");
}
System.out.println();
}
}
public static int [][] addTwoMatrixInArray(int x[][], int y[][])
{
if(x.length!=y.length||x[0].length!=y[0].length)
{
System.out.println("Addition is not possible!, Rows and columns must be same.");
return null;
}
int z[][] = new int[x.length][x[0].length];
for(int i=0;i<x.length;i++)
{
for(int j=0;j<x[i].length;j++)
{
z[i][j] = x[i][j]+y[i][j];
}
}
return z;
}
}