Tuesday, January 17, 2023

C Programming. Sum of left diagonal and right diagonal of a matrix.

Sum of left diagonal and right diagonal of a matrix.


#include<stdio.h>
int main() {
    int a[3][3];
    int sl=0,sr=0;
    printf("Enter elements of an matrix : \n");
    for (int i=0; i<3; i++){
        for (int j=0; j<3; j++){
            scanf("%d",&a[i][j]);
        }
    }
    for (int i=0; i<3; i++){
        sl=sl+a[i][i];
        sr+=a[i][2-i];
    }
    printf("sum of left diagonal : %d\nsum of right diagonal : %d",sl,sr);

    return 0;
}

Output:-

Enter elements of an matrix : 1 5 4 2 3 4 2 5 4 sum of left diagonal : 8 sum of right diagonal : 9

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...