0% found this document useful (0 votes)
29 views

Sum of Elements of Matrix

The document describes a Java program that takes user input to create a 2x2 matrix, iterates through the matrix to sum all elements, and outputs the total sum. The program uses nested for loops to input and sum the elements.

Uploaded by

Anubhav Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Sum of Elements of Matrix

The document describes a Java program that takes user input to create a 2x2 matrix, iterates through the matrix to sum all elements, and outputs the total sum. The program uses nested for loops to input and sum the elements.

Uploaded by

Anubhav Dutta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Class Matrix_Accumulate - CS_ISC 1/1

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 }

14 May, 2021 5:13:25 PM

You might also like