Program:
#include<stdio.h>
int main(){
int i,j,R,C,A[10][10],T[10][10],sum=0,sum1=0;
printf("\n{For Square Matrix... Rows=Columns}\n");
printf("Enter the No. of Rows & Columns:");
scanf("%d%d",&R,&C);
printf("Enter the Elements of the Matrix:\n");
for(i=0;i<R;i++){
for(j=0;j<C;j++){
printf("Enter Element A%d%d :",i+1,j+1 );
scanf("%d",&A[i][j]);
}
}
printf("Matrix A:");
for(i=0;i<R;i++){
printf("\n\t");
for(j=0;j<C;j++){
printf("%d ",A[i][j]);
}
printf("\n\t");
}
for(i=0;i<R;i++){
sum=sum+A[i][i];
sum1+=A[i][R-i-1]; // For OFF Diagonal Elements
}
printf("\n\t-->Sum of Diagonal Element of the Matrix A is %d. \n",sum);
printf("\n\t-->Sum of OFF Diagonal Element of the Matrix A is %d. \n",sum1);
}
Expected O/P:
{For Square Matrix... Rows=Columns}
Enter the No. of Rows & Columns:2
2
Enter the Elements of the Matrix:
Enter Element A11 :6
Enter Element A12 :7
Enter Element A21 :9
Enter Element A22 :4
Matrix A:
6 7
9 4
-->Sum of Diagonal Element of the Matrix A is 10.
-->Sum of OFF Diagonal Element of the Matrix A is 16.
0 Comments
Post a Comment