C Program to Find Sum of Opposite Diagonal Elements of a Matrix

Howdy readers, today you will learn how to write a program to find the sum of opposite diagonal elements of a matrix using C Programming language.

C Program to Find Sum of Opposite Diagonal Elements of a Matrix

The minor or opposite diagonal elements are the ones that occur from top right of matrix to bottom left corner. It is also known as secondary diagonal.

A = 4 8 2 
    7 5 1 
    2 9 5

We will find the sum of opposite diagonal elements of a matrix using a for loop statement.

So, without any delay, let’s begin this tutorial.

C Program to Find Sum of Opposite Diagonal Elements of a Matrix

C Program

// C Program to Find Sum of Opposite Diagonal Elements of a Matrix
#include <stdio.h>
int main(){
    int i, j, row, col, sum = 0;
    int arr[25][25];
    
    // Asking for input
    printf("Enter the no. of rows: ");
    scanf("%d", &row);
    printf("Enter the no. of columns: ");
    scanf("%d", &col);
    
    // Enter the elements
    printf("Enter the elements of the matrix: \n");
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            scanf("%d", &arr[i][j]);
        }
    }
    
    // Calculating sum of opposite diagonal elements
    for (i = 0; i < row; i++){
        sum = sum + arr[i][row - i - 1];
    }
    
    // Displaying output
    printf("Sum of Opposite Diagonal Elements of a Matrix: %d", sum);
    
    return 0;
}

Output

Enter the no. of rows: 3
Enter the no. of columns: 3
Enter the elements of the matrix: 
2 4 6
1 3 5
7 5 9
Sum of Opposite Diagonal Elements of a Matrix: 16
C Program to Find Sum of Opposite Diagonal Elements of a Matrix

Explanation

    int i, j, row, col, sum = 0;
    int arr[25][25];

In this program, we have declared five int data type variables and one array named arr.

    // Asking for input
    printf("Enter the no. of rows: ");
    scanf("%d", &row);
    printf("Enter the no. of columns: ");
    scanf("%d", &col);
    
    // Enter the elements
    printf("Enter the elements of the matrix: \n");
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            scanf("%d", &arr[i][j]);
        }
    }

Then, the user is asked to enter the no. of rows and columns of the matrix. After that, the user has to enter each element of the matrix.

    // Calculating sum of opposite diagonal elements
    for (i = 0; i < row; i++){
        sum = sum + arr[i][row - i - 1];
    }

Suppose, we have a matrix A = {(2, 4, 6), (1, 3, 5), (7, 5, 9)}, then:

i < row sum + arr[i][row – i – 1] = sum
0 < 30 + a[0][3-0-1] = 0 + a[0][2] = 0 + 6 = 6
1 < 36 + a[1][3-1-1] = 6 + a[1][1]  = 6 + 3 = 9
2 < 39 + a[2][3-2-1] = 9 + a[2][0] = 9 + 7 = 16
loop terminates
    // Displaying output
    printf("Sum of Opposite Diagonal Elements of a Matrix: %d", sum);

Finally, the sum of opposite diagonal elements of the matrix is printed on the screen using printf() function.

C Program to Find Sum of Opposite Diagonals Elements

C Program

// C Program to Find Sum of Opposite Diagonal Elements of a Matrix
#include <stdio.h>
int main(){
    int i, j, row, col, sum = 0;
    int arr[25][25];
    
    // Asking for input
    printf("Enter the no. of rows: ");
    scanf("%d", &row);
    printf("Enter the no. of columns: ");
    scanf("%d", &col);
    
    // Enter the elements
    printf("Enter the elements of the matrix: \n");
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            scanf("%d", &arr[i][j]);
        }
    }
    
    // Calculating sum of opposite diagonal elements
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            if (i + j == (row + 1) - 2)
                sum = sum + arr[i][j];
        }
    }
    // Displaying output
    printf("Sum of Opposite Diagonal Elements of a Matrix: %d", sum);
    
    return 0;
}

Output

Enter the no. of rows: 3
Enter the no. of columns: 3
Enter the elements of the matrix: 
8 6 4
2 5 3 
1 8 9
Sum of Opposite Diagonal Elements of a Matrix: 10
C Program to Find Sum of Opposite Diagonal Elements of a Matrix

Conclusion

I hope after reading this tutorial, you will understand how to write a program to find the sum of opposite diagonal elements of a matrix using C Programming language.

If you face any difficulty while understanding this tutorial, then let us know in the comment section. We will be glad to solve your doubts.

Leave a Comment