C program to find the sum of opposite diagonal elements of a matrix

Write a C program to read elements in a matrix and find the sum of minor diagonal (opposite diagonal) elements. C program to calculate sum of minor diagonal elements. Logic to find sum of opposite diagonal elements of a matrix in C programming.

Example

Input

Input elements in array:
1 2 3
4 5 6
7 8 9

Output

Sum of minor diagonal elements = 15

Required knowledge

Basic C programming, For loop, Array

Minor diagonal of a matrix

Minor diagonal of a matrix A is a collection of elements Aij Such that i + j = N + 1.

Minor diagonal of a matrix

Read more – Program to find sum of main diagonal element of a matrix

Program to find sum of opposite diagonal elements of a matrix

/**
 * C program to find sum of opposite diagonal elements of a matrix
 */

#include <stdio.h>

#define SIZE 3 // Matrix size

int main()
{
    int A[SIZE][SIZE];
    int row, col, sum = 0;

    /* Input elements in matrix from user */
    printf("Enter elements in matrix of size %dx%d: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

    /* Find sum of minor diagonal elements */
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            /*
             * If it is minor diagonal of matrix
             * Minor diagonal: i+j == N + 1
             * Since array elements starts from 0 hence i+j == (N + 1)-2
             */
            if(row+col == ((SIZE+1)-2))
            {
                sum += A[row][col];
            }
        }
    }

    printf("\nSum of minor diagonal elements = %d", sum);

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 2 3
4 5 6
7 8 9

Sum of minor elements = 15

Happy coding 😉