C Program to Addition of Diagonal Elements in Matrix | C Program to Diagonally Add elements | Diagonal addition of elements of matrix in C
#include
#include
void main()
{
int i,j,a[10][10],sum,m,n;
/* m - Number of rows
n - Number of Columns */
printf("\n Enter the number of Rows : ");
scanf ("%d",&m);
printf("\n Enter the number of Columns : ");
scanf ("%d",&n);
/* Accept the Elements in m x n Matrix */
for(i=0;i<m;i++ )
for(j=0;j<n;j++)
{
printf("Enter the Element a[%d][%d] : ", i , j);
scanf("%d",&a[i][j]);
}
/* Addition of all Diagonal Elements */
sum = 0;
for(i=0;i<m;i++ )
for(j=0;j<n;j++)
{
if ( i == j )
sum = sum + a[i][j];
}
/* Print out the Result */
printf("\nSum of All Diagonal Elements in Matrix : %d",sum);
getch();
}
Enter the number of Rows : 2
Enter the number of Columns : 2
Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 1
Enter the Element a[1][0] : 1
Enter the Element a[1][1] : 1
The Addition of All Elements in the Matrix : 2
No comments:
Post a Comment