
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Compute the Sum of Diagonals of a Matrix
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
- Related Articles
- Golang Program to Compute the Sum of Diagonals of a Matrix
- Swift Program to Compute the Sum of Diagonals of a Matrix
- C++ Program to Compute the Sum of Diagonals of a Matrix
- JavaScript Program to Efficiently compute sums of diagonals of a matrix
- Swift Program to Interchange the Diagonals of a Matrix
- C++ Program to Compute Determinant of a Matrix
- Program to Interchange Diagonals of Matrix in C program
- PyTorch – How to compute the pseudoinverse of a matrix?
- C Program to print the sum of boundary elements of a matrix
- Java Program to Interchange the Diagonals
- Swift Program to Find the Sum of the Boundary Elements of a Matrix
- Swift Program to Calculate the Sum of Matrix Elements
- PyTorch – How to compute the determinant of a square matrix?
- PyTorch – How to compute the inverse of a square matrix?
- Java program to print the transpose of a matrix

Advertisements