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


March 1, 2022, Learn eTutorial
1534

What are the diagonal and off-diagonal of a matrix?

The main diagonal of the matrix in a square matrix, the elements from the left upper corner connecting through the central element to the rightmost bottom element is called the main diagonal of that matrix. 

The anti diagonal of the matrix in a square matrix, the elements from the right upper corner connecting through the central element to the leftmost bottom element is called the anti diagonal of that matrix. 

diagonal and off diagonal of a 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.

How to calculate the sum of the main diagonal and anti-diagonal of the matrix using C program?

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'.

matrix elements and positions

finally, we display both the sums as a result.

Note: 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 anti-diagonal elements as a.

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


This matrix program uses the below c programming topics, please refer these topics for a better understanding

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 anti-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 anti-diagonal elements is   = 15

Run 2

Enter the order of the matrix
2 3

The given order is not square matrix