**JAVA PROGRAMS**

**# Java Array Tutorials**

**# Java Array Programs**

➤ Find Length of Array

➤ Different ways to Print Array

➤ Sum of Array Elements

➤ Average of Array Elements

➤ Sum of Two Arrays Elements

➤ Compare Two Arrays in Java

➤ 2nd Largest Number in Array

➤ How to Sort an Array in Java

➤ Reverse an Array in Java

➤ GCD of N Numbers in Java

➤ Linear Search in Java

➤ Binary Search in Java

➤ Copy Array in Java

➤ Merge 2 Arrays in Java

➤ Merge two sorted Arrays

➤ Largest Number in Array

➤ Smallest Number in Array

➤ Remove Duplicates

➤ Insert at Specific Position

➤ Add Element to Array

➤ Remove Element From Array

➤ Count Repeated Elements

➤

**More Array Programs**

**Java Matrix Programs**

➤ Matrix Tutorial in Java

➤ Print 2D Array in Java

➤ Print a 3×3 Matrix

➤ Sum of Matrix Elements

➤ Sum of Diagonal Elements

➤ Row Sum – Column Sum

➤ Matrix Addition in Java

➤ Matrix Subtraction in Java

➤ Transpose of a Matrix in Java

➤ Matrix Multiplication in Java

➤ Menu-driven Matrix Operations

Program description:- **Write a Java program to find the sum of matrix elements. Take a matrix, use a method to find the sum, and display the result**.

Example:-

Matrix =

1 2 3

4 5 6

7 8 9

Sum of matrix elements = 45

Before solving this problem, you should have knowledge of how to declare and initialize a matrix in Java, how to take input for a matrix from the end-user, and what are the different ways to display it. How to find the length or size of a matrix in Java? How to pass and return a matrix in Java. See:- Matrix in Java

**Procedure to find the sum of matrix elements**,

a) Take a matrix.

b) Declare a sum variable and initialize it with 0.

c) Traverse through the matrix.

d) Access each element of the matrix and add it to the sum variable.

e) Display the sum value.

## Java Program to Find the Sum of Matrix Elements

```
public class Matrix {
// main method
public static void main(String[] args) {
// declare and initialize matrix
int a[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// find sum of matrix elements
int sum = matrixSum(a);
// display result
System.out.println("The sum of matrix elements = " + sum);
}
// method to find sum of matrix elements
public static int matrixSum(int[][] a) {
// variable to store sum
int sum = 0;
// traverse through the matrix
for (int[] row : a) {
for (int element : row) {
// add element
sum += element;
}
}
// return sum value
return sum;
}
}
```

Output:-

The sum of matrix elements = 45

See more matrix programs in Java:-

- Program to Print 3×3 Matrix
- Sum of Diagonal Elements of Matrix in Java
- Row sum and Column sum of Matrix in Java
- Matrix Addition in Java
- Subtraction of two matrices in Java
- Transpose of a Matrix in Java
- Matrix Multiplication in Java
- Menu-driven program for Matrix operations

Also See:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!