-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddMatrix.java
47 lines (36 loc) · 1.15 KB
/
AddMatrix.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
/* Write a program to add two matrices */
import java.util.Scanner;
class AddMatrix{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows in a matrix : ");
int rows = sc.nextInt();
System.out.print("Enter the number of columns in a matrix : ");
int cols = sc.nextInt();
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
int[][] sum = new int[rows][cols];
//first matrix
System.out.println("Enter values for first matrix : ");
for(int i= 0;i<rows;i++)
for(int j=0;j<cols;j++)
matrix1[i][j] = sc.nextInt();
//second matrix
System.out.println("Enter values for second matrix : ");
for(int i = 0;i<rows;i++)
for (int j = 0;j<cols;j++)
matrix2[i][j] = sc.nextInt();
// add matrix
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
sum[i][j] = matrix1[i][j]+matrix2[i][j];
//print after sum
System.out.println("Sum of two matrices : ");
for(int i= 0;i<rows;i++){
for(int j=0;j<cols;j++){
System.out.print(sum[i][j]+" ");
}
System.out.println();
}
}
}