Here, we will share information about a C Program, which will find the Sum of Corner Elements in a Matrix. This is only possible in a two-dimensional array because table form or matrix form is only possible in a two-dimensional array. We will first create a matrix with the help of a 2d array.
Sum of Corner Elements
In the previous article, we discussed C Program to Find the Sum of Each Row of a Matrix, but here we will only discuss about how to find the Sum of Corner Elements in a matrix. For that, first, we have to create a two-dimensional array, then we can see their sum using the C language easily.
Program
#include<stdio.h> int main() { int size,sum=0,a[10][10],i,j; printf("enter size of matrix: "); scanf("%d",&size); printf("enter elements of matrix: "); for(i=0;i<size;i++) { for(j=0;j<size;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<size;i++) { for(j=0;j<size;j++) { printf("%d\t",a[i][j]); } printf("\n"); } a[i][j]= a[0][0] + a[0][i-1] +a[j-1][0] + a[j-1][i-1]; printf("sum of matrix is : %d", a[i][j]); return 0; }
Output
First of all, we will declare integer type variables which are size, a[10][10], I, and j and we will initialize the sum as 0. A size variable is used for taking the size of the matrix from the user. Then we will take another variable for taking values of the matrix from the user.
Other variables, which we declared are I and j. these variables are for applying the loop. The sum is initialized as 0 to avoid garbage value. Then we will first take the size of the two-dimensional array from the user.
Then after taking the size, we will take elements of the array from the user to make a matrix. Then we will first print out these elements which will be in the form of a matrix or table. Then we will apply the formula of the sum which will find the sum of corner elements of a matrix.
Conclusion
Above C program will find out the Sum of Corner Elements of a two-dimensional array. This method will only find the sum of corner elements of a matrix. If you feel any problems related to coding then feel free to contact us.