# C Program to Find Sum of Diagonal Elements of Matrix

• Write a C program to find sum of diagonal elements of a matrix.

An element A[i][j] of matrix A is said to be diagonal element, if i == j. For a matrix A of size 3 X 3, A[0][0], A[1][1] and A[2][2] are diagonal elements of A. Given a matrix of size M x N, we have to find the sum of all diagonal elements of given matrix.
Algorithm to find sum of diagonal matrix

• Initialize a variable with zero(Lets call it DiagonalSum).
• Traverse each element of the matrix using two loops.
• If row index and column index of an element is same, then this element is a diagonal element and add it to DiagonalSum.
• Once we complete matrix traversal, we will get sum of all diagonal elements in DiagonalSum variable.

### C program to find sum of diagonal elements of a matrix

Below program first takes the dimension of matrix(rows and columns) as input from user. Then it takes elements of matrix as input using two for loops and scanf function. For every element inputMatrix[rowCounter][colCounter], it checks whether rowCounter is equal to colCounter or not. If equal, then it adds the corresponding elements value to diagonalSum.

/*
* C Program to find sum of diagonal elements of matrix
*/

#include <stdio.h>
#include <conio.h>

int main(){
int rows, cols, rowCounter, colCounter, diagonalSum = 0;
int inputMatrix[50][50];
printf("Enter Rows and Columns of Matrix\n");
scanf("%d %d", &rows, &cols);

printf("Enter first Matrix of size %dX%d\n", rows, cols);
/*  Input first matrix*/
for(rowCounter = 0; rowCounter < rows; rowCounter++){
for(colCounter = 0; colCounter < cols; colCounter++){
scanf("%d", &inputMatrix[rowCounter][colCounter]);
}
}
/* Sum diagonal elements of input matrix. Diagonal elements are those
elements whose row and column indexes are same. */
for(rowCounter = 0; rowCounter < rows; rowCounter++){
for(colCounter = 0; colCounter < cols; colCounter++){
if(rowCounter == colCounter){
diagonalSum += inputMatrix[rowCounter][colCounter];
}
}
}

printf("Sum of all diagonal elements of Matrix is: %d\n", diagonalSum);
getch();
return 0;
}

Program Output

Enter Rows and Columns of Matrix
2 3
Enter first Matrix of size 2X3
1 2 3
4 5 6
Sum of all diagonal elements of Matrix is: 6

Enter Rows and Columns of Matrix
3 3
Enter first Matrix of size 2X3
0 1 1
2 -1 4
3 1 2
Sum of all diagonal elements of Matrix is: 1


### C program to find sum of diagonal elements of a matrix without traversing whole matrix

Below program doesn’t traverse whole matrix, instead it only visits diagonal elements and adds their values to diagonalSum. It uses the fact that, row and column indexes of every diagonal elements are equal.

For example, inputMatrix[0][0], inputMatrix[1][1], inputMatrix[2][2]….. are diagonal elements of inputMatrix.

/*
* C Program to find sum of diagonal elements of matrix
* without traversing whole matrix
*/

#include <stdio.h>
#include <conio.h>

int main(){
int rows, cols, rowCounter, colCounter, diagonalSum = 0;
int inputMatrix[50][50];
printf("Enter Rows and Columns of Matrix\n");
scanf("%d %d", &rows, &cols);

printf("Enter first Matrix of size %dX%d\n", rows, cols);
/*  Input first matrix*/
for(rowCounter = 0; rowCounter < rows; rowCounter++){
for(colCounter = 0; colCounter < cols; colCounter++){
scanf("%d", &inputMatrix[rowCounter][colCounter]);
}
}
/* Sum diagonal elements of input matrix. Diagonal elements are those
elements whose row and column indexes are same.
For Example: Matrix[1][1], Matrix[4][4] */
for(rowCounter = 0; rowCounter < rows; rowCounter++){
if(rowCounter <= cols-1) {
diagonalSum += inputMatrix[rowCounter][rowCounter];
}
}

printf("Sum of all diagonal elements of Matrix is: %d\n", diagonalSum);
getch();
return 0;
}

Program Output

Enter Rows and Columns of Matrix
3 3
Enter first Matrix of size 3X3
1 2 3
4 5 6
9 5 2
Sum of all diagonal elements of Matrix is: 8