-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixsum.java
35 lines (26 loc) · 989 Bytes
/
Matrixsum.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
/* Problem
Given a matrix of size n x m. Your task is to find sum of all the elements in the matrix.
Input Format:First line contains two space seprated integers n and m.
Next n lines contains m space seprated integers denoting the elements in the matrix.*/
import java.util.*;
public class Matrixsum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int rows = scanner.nextInt();
int columns = scanner.nextInt();
int[][] matrix = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = scanner.nextInt();
}
}
// Calculate the sum of all elements
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
sum += matrix[i][j];
}
}
System.out.println(sum);
}
}