# Java Program to Compute the Sum of Diagonals of a Matrix

JavaCampus InterviewServer Side ProgrammingProgramming

#### Complete Java Programming Fundamentals With Sample Projects

98 Lectures 7.5 hours

#### Get your Java dream job! Beginners interview preparation

85 Lectures 6 hours

#### Core Java bootcamp program with Hands on practice

Featured

99 Lectures 17 hours

In this article, we will understand how to compute the sum of diagonals of a matrix. The matrix has a row and column arrangement of its elements. The principal diagonal is a diagonal in a square matrix that goes from the upper left corner to the lower right corner.

The secondary diagonal is a diagonal of a square matrix that goes from the lower left corner to the upper right corner.

Below is a demonstration of the same −

Suppose our input is

The input matrix:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The desired output would be

Sum principal diagonal elements: 74
Sum of secondary diagonal elements: 45

## Algorithm

Step 1 - START
Step 2 - Declare an integer matrix namely input_matrix
Step 3 - Define the values.
Step 4 - Iterate over each element of the matrix using two for-loops, add the diagonal elements use the condition ‘i’ = ‘j’ to get the sum of principle diagonal elements and the condition ‘i’ + ‘j’ = matrix_size – 1 to get the sum of secondary diagonal elements.
Step 5 - Display the result
Step 5 - Stop

## Example 1

Here, we bind all the operations together under the ‘main’ function.

public class MatrixDiagonals {
static public void main(String[] args) {
int[][] input_matrix = {
{ 4, 5, 6, 7 },
{ 1, 7, 3, 4 },
{ 11, 12, 13, 14 },
{ 23, 24, 25, 50 }
};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
int principal_diagonal = 0, secondary_diagonal = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
principal_diagonal += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
secondary_diagonal += input_matrix[i][j];
}
}
System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);
System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
}
}

## Output

The matrix is defined as :
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The sum of principal diagonal elements of the matrix is: 74

The sum of secondary diagonal elements of the matrix is: 45

## Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class MatrixDiagonals {
static void diagonals_sum(int[][] input_matrix, int matrix_size) {
int principal_diagonal = 0, secondary_diagonal = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
principal_diagonal += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
secondary_diagonal += input_matrix[i][j];
}
}
System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);

System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
}
static public void main(String[] args) {
int[][] input_matrix = {
{ 4, 5, 6, 7 },
{ 1, 7, 3, 4 },
{ 11, 12, 13, 14 },
{ 23, 24, 25, 50 }
};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
diagonals_sum(input_matrix, matrix_size);
}
}

## Output

The matrix is defined as:
4 5 6 7
1 7 3 4
11 12 13 14
23 24 25 50

The sum of principal diagonal elements of the matrix is:74

The sum of secondary diagonal elements of the matrix is: 45
Updated on 29-Mar-2022 09:12:17