C Program to find sum of main diagonal and off diagonal elements of a matrix

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to calculate the sum of the main diagonal and off-diagonal of the matrix?

In this c program, what we need is to calculate the sum of the main diagonal and the off-diagonal sum separately. That means we need to calculate the sum of diagonals of the matrix. Also, the sum of the off-diagonal elements of the matrix.

Example: if A is the Matrix

[5 2
2 3]

  • The Main diagonal sum of the Matrix A = 5 + 3 = 8.
  • The Off diagonal sum of the Matrix A = 2 + 2 = 4.

Here in this c program, we accept a matrix and print the matrix as entered using for loop, now we open a for loop, inside that for loop, we add the elements of the main diagonal to a variable called sum. Also, we will find out the sum of the off-diagonal elements to the variable 'a'.

finally, we display both the sums as a result. One thing we need to remember is that we can only do this operation if the rows and columns are equal. So first, we check that using an if condition. If the rows and columns are not matching, then display an error message.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the variable i, j, m, n, a, sum, and the a=0, sum=0.

STEP 3: Read the Order of the Matrix into the variable m and n.

STEP 4: Check if m=n then do step 5.Else do step 10.

STEP 5: Read the Coefficients of the Matrix into ma[i][j] using nested for loop.

STEP 6: Display the given Matrix as each element of ma[i[j] using printf function.

STEP 7: By using a for loop Calculate the sum of the matrix as sum=sum+ma[i][j],a=a+ma[i][m-i-1].

STEP 8: Display the Sum of the main diagonal elements as Sum.

STEP 9: Display the Sum of the off-diagonal elements as a.

STEP 10: If the order of the Matrix is not equal, then display the order is not a square matrix.

C Source Code

                                          #include <stdio.h>

void main() {
  static int ma[10][10];
  int i, j, m, n, a = 0, sum = 0;
  printf("Enter the order of the matrix \n"); /* accepting the matrix order */
  scanf("%d %d", & m, & n);
  if (m == n) {
    printf("Enter the coefficient s of the matrix\n"); /* enters the elements of the matrix */
    for (i = 0; i < m; ++i) {
      for (j = 0; j < n; ++j) {
        scanf("%d", & ma[i][j]);
      }
    }
    printf("The given matrix is \n"); /* prints the matrix as such */
    for (i = 0; i < m; ++i) {
      for (j = 0; j < n; ++j) {
        printf(" %d", ma[i][j]);
      }
      printf("\n");
    }
    for (i = 0; i < m; ++i) {
      sum = sum + ma[i][i]; /*calculating the sum of main diagonal and off diagonal separately*/
      a = a + ma[i][m - i - 1];
    }
    printf("\n The sum of the main diagonal elements is = %d\n", sum);
    printf("The sum of the off-diagonal elements is   = %d\n", a);
  } else
    printf("The given order is not square matrix\n"); /*cannot calculate for a non square matrix*/
} /* End of main() */
                                      

OUTPUT

Enter the order of the matrix
3*3

Enter the coefficients of the matrix
1 2 3
4 5 6
7 8 9

The given matrix is
 1 2 3
 4 5 6
 7 8 9

The sum of the main diagonal elements is = 15
The sum of the off-diagonal elements is   = 15

Run 2

Enter the order of the matrix
2 3

The given order is not square matrix