top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING C?

+2 votes
284 views
FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING C?
posted Jan 20, 2016 by Jayshree

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

Sum of diagonal elements of a matrix in c

#include<stdio.h>

int main(){

  int a[10][10],i,j,sum=0,m,n;

  printf("\nEnter the row and column of matrix: ");
  scanf("%d %d",&m,&n);

  printf("\nEnter the elements of matrix: ");
  for(i=0;i<m;i++)
      for(j=0;j<n;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");

  for(i=0;i<m;i++){
      printf("\n");
      for(j=0;j<m;j++){
      printf("%d\t",a[i][j]);
      }
 }
 for(i=0;i<m;i++){
     for(j=0;j<n;j++){
          if(i==j)
              sum=sum+a[i][j];
     }
 }
 printf("\n\nSum of the diagonal elements of a matrix is: %d",sum);

 return 0;
}

Sample output:

Enter the row and column of matrix: 3 3
Enter the elements of matrix: 2
3
5
6
7
9
2
6
7
The matrix is
2       3       5
6       7       9
2       6       7
Sum of the diagonal elements of a matrix is: 16

Alogrithm:

Sum of diagonal element of matrix:

enter image description here

Diagonal elements have been shown in the bold letter.

We can observer the properties any element Aij will diagonal element if and only if i = j

answer Jan 20, 2016 by Shivaranjini
Similar Questions
+2 votes

How to sort elements in a matrix that are above the principal diagonal using C/C++, diagonal elements are also included?

0 votes

For given list of numbers find out triplets with sum 0 using C?
Input : arr[] = {0, -1, 2, -3, 1}
Output : 0 -1 1
2 -3 1

+1 vote

Given a array of integers there is one that is repeated several time. How would you compute the length of the sequence of repeated elements.

0 votes

Given an unordered array A of size n and integer x. What is the best complexity to find two elements in A whose sum is x?
Share the algo, its complexity and if possible C code.

...