C Program to find the Sum of Diagonal Elements in a n x n Matrix

 


// Program by Akash Tripathi (@proakash256)
 
#include <stdio.h>
int main()
{
    int r , s = 0 , a = 0;
    printf("Enter the number of Rows and Columns: ");
    scanf("%d" , &r);
    int ar[r][r];
    printf("\nEnter the elements (row wise):\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
        {
            scanf("%d" , &ar[i][j]);
            if(i == j)
                s = s + ar[i][j];
            if(j == (r - 1 - i))
                a = a + ar[i][j];
        }
    }
    printf("\nThe given matrix is :\n\n");
    for(int i = 0i < ri = i + 1)
    {
        for(int j = 0j < rj = j + 1)
            printf("%d\t" , ar[i][j]);
        printf("\n");
    }
    printf("\nThe Sum of the Right Diagonal elements 
of the given Matrix is %d\n\n" , s);
    printf("\nThe Sum of the Left Diagonal elements 
of the given Matrix is %d\n\n" , a);
    return 0;
}

Comments