Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save naemazam/a5fc8fcc456e4f0a77d71ad79cf5e090 to your computer and use it in GitHub Desktop.
Save naemazam/a5fc8fcc456e4f0a77d71ad79cf5e090 to your computer and use it in GitHub Desktop.
computing the sums of two diagonals and sum diagonal of a matrix in c language
\\\ sum of main diagonal
/// sum of sub diagonal
#include <stdio.h>
void main ()
{
static int array[10][10];
int i, j, m, n, a = 0, sum = 0;
printf("Enetr the order of the matix \n");
scanf("%d %d", &m, &n);
if (m == n )
{
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}
for (i = 0; i < m; ++i)
{
sum = sum + array[i][i];
a = a + array[i][m - i - 1];
}
printf("\nThe sum of the main diagonal elements is = %d\n", sum);
printf("The sum of the off diagonal elements is = %d\n", a);
}
else
printf("The given order is not square matrix\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment