Posted By:
23 Jun, 2013 1:09 pm PDT

Arrays in C

How can you find the sum of diagonals on a matrix?
Attachment:None


To find the sum of the diagonals of a given matrix we use the following code:

 

#include "stdio.h"
#include "conio.h"
#include"stdlib.h"


int main()
{
int a[10][10],sum=0;
int i,j,r1,c1;

printf("\n Enter length of Marix A row by coloum : ");
scanf("%d%d",&r1,&c1);

if((r1>10) || (c1>10))
{
 printf("\n Input length is more than declared \n");
  exit(1);
}

printf("\n Enter elements of Matrix- A Row-wise.\n");
for(i=0;i<r1;i++)
  for(j=0;j<c1;j++)
     scanf("%d",&a[i][j]);

for(i=0;i<r1;i++)
 {
 for(j=0;j<c1;j++)  {
  if(i == j)
   sum += a[i][j];
 }
}
printf("\n Sum of Diagonal elements of Matrix is : %d ",sum);
getch();
}

 

 Please see the video for more details.

Was it useful?  



1  2  3
4  5  6
7  8  9
 
In above 3X3 matrix there are two types diagonal element as:
first diagonal elements are  1, 5, 9 and
second diagonal elements are 3, 5,7
 
please find attached the program to find the sum of diagonals of a matrix.

Was it useful?  




Please login to reply to this problem.