Home Java Program Examples Program to find the diagonal sum of a matrix

Program to find the diagonal sum of a matrix

by anupmaurya
5 minutes read

In this article, you’ll learn to make Program to find the diagonal sum of a matrix in different programming languages.

What is Matrix?

Matrix, a set of numbers arranged in rows and columns so as to form a rectangular array. The numbers are called the elements, or entries, of the matrix.

For example, consider the following 3 X 3 input matrix.

A00 A01 A02 
A10 A11 A12 
A20 A21 A22

The primary diagonal is formed by the elements A00, A11, A22
Condition for Principal Diagonal: The row-column condition is row = column. 
The secondary diagonal is formed by the elements A03, A12, A21
Condition for Secondary Diagonal: The row-column condition is row = numberOfRows – column -1
Input : 
4
2 2 3 4
4 3 2 1
7 8 9 6
6 5 4 4
Output :
Principal Diagonal: 18
Secondary Diagonal: 20

Input :
3
2 2 2
2 2 2
2 2 2
Output :
Principal Diagonal: 6
Secondary Diagonal: 6

Program to find the diagonal sum of a matrix in C++

// A simple C++ program to find sum of diagonals
#include <bits/stdc++.h>
using namespace std;

const int MAX = 100;

void printDiagonalSums(int mat[][MAX], int n)
{
	int principal = 0, secondary = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {

			// Condition for principal diagonal
			if (i == j)
				principal += mat[i][j];

			// Condition for secondary diagonal
			if ((i + j) == (n - 1))
				secondary += mat[i][j];
		}
	}

	cout << "Principal Diagonal:" << principal << endl;
	cout << "Secondary Diagonal:" << secondary << endl;
}

// Driver code
int main()
{
	int a[][MAX] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 },
					{ 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
	printDiagonalSums(a, 4);
	return 0;
}

Program to find the diagonal sum of a matrix in Java

// A simple java program to find
// sum of diagonals
import java.io.*;

public class Diagonals {

	static void printDiagonalSums(int [][]mat,
										int n)
	{
		int principal = 0, secondary = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
	
				// Condition for principal
				// diagonal
				if (i == j)
					principal += mat[i][j];
	
				// Condition for secondary
				// diagonal
				if ((i + j) == (n - 1))
					secondary += mat[i][j];
			}
		}
	
		System.out.println("Principal Diagonal:"
									+ principal);
									
		System.out.println("Secondary Diagonal:"
									+ secondary);
	}

	// Driver code
	static public void main (String[] args)
	{
		
		int [][]a = { { 1, 2, 3, 4 },
					{ 5, 6, 7, 8 },
					{ 1, 2, 3, 4 },
					{ 5, 6, 7, 8 } };
					
		printDiagonalSums(a, 4);
	}
}


Program to find the diagonal sum of a matrix in Python

# A simple Python program to
# find sum of diagonals
MAX = 100

def printDiagonalSums(mat, n):

	principal = 0
	secondary = 0;
	for i in range(0, n):
		for j in range(0, n):

			# Condition for principal diagonal
			if (i == j):
				principal += mat[i][j]

			# Condition for secondary diagonal
			if ((i + j) == (n - 1)):
				secondary += mat[i][j]
		
	print("Principal Diagonal:", principal)
	print("Secondary Diagonal:", secondary)

# Driver code
a = [[ 1, 2, 3, 4 ],
	[ 5, 6, 7, 8 ],
	[ 1, 2, 3, 4 ],
	[ 5, 6, 7, 8 ]]
printDiagonalSums(a, 4)


Program to find the diagonal sum of a matrix in JavaScript

<script>
// A simple Javascript program to find sum of diagonals

const MAX = 100;

void printDiagonalSums(mat, n)
{
	let principal = 0, secondary = 0;
	for (let i = 0; i < n; i++) {
		for (let j = 0; j < n; j++) {

			// Condition for principal diagonal
			if (i == j)
				principal += mat[i][j];

			// Condition for secondary diagonal
			if ((i + j) == (n - 1))
				secondary += mat[i][j];
		}
	}

	document.write("Principal Diagonal:" + principal + "<br>");
	document.write("Secondary Diagonal:" + secondary + "<br>");
}

// Driver code
	let a = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ],
					[ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ];
	printDiagonalSums(a, 4);

</script>

Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.