# Program to find the diagonal sum of a matrix

0 comment 264 views

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++

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```// 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;
}
```Code language: PHP (php)```

## 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);
}
}

```Code language: JavaScript (javascript)```

## 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)

```Code language: PHP (php)```

## 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>
```Code language: HTML, XML (xml)```

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!