Square Matrix-Corner Elements Sum

Square Matrix-Corner Elements Sum

A square matrix of size N*N is passed as the input.The program must calculate and print the sum of the elements in the corners.
Sample Input:
Enter the order N of matrix : 3
Enter the matrix elements :
10 9 1
4 5 4
32 8 66
Sample Output:
109
Explanation:
sum = 10+1+32+66=109
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<stdio.h>
int main()
{
    int sum=0,a[10][10],i,j,n;
    printf("Enter the order N of matrix : ");
    scanf("%d",&n); //n*n matrix
    printf("Enter the matrix elements : ");
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<n;i++)
    {
        if((i==0)||(i==n-1)) //select only first and last row
        {
            for(j=0;j<n;j++)
            {
                if((j==0)||(j==n-1)) //select only first and last column 
                {
                    sum=sum+a[i][j];
                }
            }
        }
    }
    printf("%d",sum);
    return 0;
    
}

OUTPUT:


0 comments