Sum of Elements of Matrix
Sum of Elements of Matrix
1 import java.util.*;
2 public class Matrix_Accumulate
3 {
4 public static void main(String[] args)
5 {
6 Scanner sc=new Scanner(System.in);
7 int a[][] =new int[2][2];
8 int rows=a.length;
9 int cols=a[0].length;
10 int sum=0;
11 System.out.println("Enter Matrix:");
12 for(int i = 0; i < rows; i++)
13 {
14 for(int j = 0; j < cols; j++)
15 {
16 a[i][j]=sc.nextInt();
17 }
18 }
19 for(int i = 0; i < rows; i++)
20 {
21 for(int j = 0; j < cols; j++)
22 {
23 sum += a[i][j];
24 }
25 }
26 System.out.println("Sum of Elements:"+sum);
27 }
28 }