Skip to content

Instantly share code, notes, and snippets.

@sivsivsree
Created June 15, 2020 05:45
Show Gist options
  • Save sivsivsree/c116dfc8dcb502b89647c965bceab0af to your computer and use it in GitHub Desktop.
Save sivsivsree/c116dfc8dcb502b89647c965bceab0af to your computer and use it in GitHub Desktop.
#include<stdio.h>
#define SIZE 3 // 3x3 matrix
int main()
{
int A[SIZE][SIZE];
int row, col, sum = 0;
int DSum = 0;
printf("Enter elements in matrix of size %dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE; row++){
for(col=0; col<SIZE; col++){
scanf("%d", &A[row][col]);
}
}
/* Calculate sum of elements of each row of matrix */
for(row=0; row<SIZE; row++){
sum = 0;
for(col=0; col<SIZE; col++){
sum += A[row][col];
}
printf("Sum of elements of Row %d = %d\n", row+1, sum);
}
for(row=0; row<SIZE; row++){
sum = 0;
for(col=0; col<SIZE; col++){
sum += A[col][row];
}
printf("Sum of elements of Column %d = %d\n", row+1, sum);
}
for(row = 0; row < SIZE; row++){
DSum = DSum + A[row][row];
}
printf("\nThe diagonal sum = %d", DSum );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment