# Sum of Diagonal Elements of a Matrix in C

Program Description:- Write a program to find the sum of diagonal elements of a matrix in C.

Procedure to find the sum of diagonal elements of a given matrix,
a) Take a matrix
b) Declare a sum variable and initialized with 0.
c) Iterate through each element of the matrix.
d) When the element is a diagonal element then add it to the sum variable.
e) Display the sum variable.

If the given matrix is a square matrix (whose row and column are equal) then the sum of diagonal elements is also called the trace of the matrix.

Now, let us see the simple C program to find the sum diagonal elements. In this program, matrix elements are given in the program itself.

``````#include <stdio.h>
// main function
int main()
{
// matrix A
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

// declare sum variable
int sum = 0;

// iterate through the matrix
// outer loop for row
for (int i = 0; i < 3; i++)
{
// inner loop for column
for (int j = 0; j < 3; j++)
{
// condition to find diagonal element
if(i == j) {
// calculate sum
sum += matrix[i][j];
}
}
}

// display matrix
printf("Matrix = \n");
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n"); // new line
}

// display sum of diagonal elements
printf("Sum of diagonal elements = %d\n", sum);

return 0;
}``````

Output:-

Matrix =
1 2 3
4 5 6
7 8 9
Sum of diagonal elements = 15

Time complexity:- O(N2)

To iterate through the matrix, it requires two loops therefore if we assume the given matrix is a square matrix then the time complexity for the above program will be O(N2).

## Sum of Diagonal Elements of a Matrix in C using Function by taking input value

In the previous program, matrix elements were initialized within the program itself. Now, let us develop another C program by taking input for matrix elements from the user and using functions.

``````#include <stdio.h>
// function to find sum of
// digonal elements of a matrix
int diagonalSum(int matrix[10][10], int row, int column)
{
// declare variable
int sum = 0;

// iterate through the matrix
// outer loop for row
for (int i = 0; i < 3; i++)
{
// inner loop for column
for (int j = 0; j < 3; j++)
{
// condition to find diagonal element
if(i == j) {
// calculate sum
sum += matrix[i][j];
}
}
}

// return sum
return sum;
}
void readMatrix(int matrix[10][10], int row, int column)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < column; ++j)
{
scanf("%d", &matrix[i][j]);
}
}
}
// main function
int main()
{
// declare matrix matrix A, B, & C
int matrix[10][10]; // first matrix

// read the size of matrices
int row, column;
printf("Enter Row and Column for Matrix: ");
scanf("%d %d", &row, &column);

printf("Enter Matrix Elements: \n");

// find sum of diagonal elements
printf("Sum of diagonal elements = %d\n",
diagonalSum(matrix, row, column) );

return 0;
}``````

Output:-

Enter Row and Column for Matrix: 3 2
Enter Matrix Elements:
9 8
7 4
5 6
Sum of diagonal elements = 13

Also See:-

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!