Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 4

C Program to find Sum of Diagonal

Elements of a Matrix
/* C Program to find Sum of Diagonal Elements of a Matrix */
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );

return 0;
}
C Program to do the Sum of the Main & Opposite Diagonal
Elements of a MxN Matrix
/*
* C program to find accept a matrix of order M x N and find
* the sum of the main diagonal and off diagonal elements
*/

#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)
{ for (i = 0; i < m; ++i)
scanf("%d", &array[i][j]); {
} sum = sum + array[i][i];
a = a + array[i][m - i - 1];
}
}
printf("The given matrix is \n"); printf("\nThe sum of the main diagonal elements is
for (i = 0; i < m; ++i) = %d\n", sum);
{ printf("The sum of the off diagonal elements is =
for (j = 0; j < n; ++j) %d\n", a);
{
printf(" %d", array[i][j]); }
}
else
printf("\n");
printf("The given order is not square matrix\n");
}
}

You might also like