FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING








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:















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









15 comments:

  1. It is my luck..i am searched alot for this program in a single loop..thanks.

    ReplyDelete
  2. thank u................

    ReplyDelete
  3. can u give d code using pointers???

    ReplyDelete
  4. i am very happy to get my all pl matrix prog
    thank you

    ReplyDelete
  5. Thanks a lot for your programs it helped me a lot.Could you post the progra to find the anti diagonal elements in a matrix.Thanks in advance.

    ReplyDelete
  6. if t want add the sum of both diagonals,then how can i do?

    ReplyDelete
  7. #include
    #include
    void main()
    {
    int a[10][10],b[10][10],c[10][10],n,i,j;
    printf("Enter the value of n=>");
    scanf("%d",&n);
    printf("Enter the First matrix->");
    for(i=0;i");
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    scanf("%d",&b[i][j]);
    printf("\nThe First matrix is\n");
    for(i=0;i<n;i++){
    printf("\n");
    for(j=0;j<n;j++)
    printf("%d\t",a[i][j]);
    }
    printf("\nThe Second matrix is\n");
    for(i=0;i<n;i++){
    printf("\n");
    for(j=0;j<n;j++)
    printf("%d\t",b[i][j]);
    }
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    c[i][j]=a[i][j]+b[i][j];
    printf("\nThe Addition of two matrix is\n");
    for(i=0;i<n;i++){
    printf("\n");
    for(j=0;j<n;j++)
    printf("%d\t",c[i][j]);
    }
    getch();
    }

    ReplyDelete
  8. sweet thanks a lot.....................................thousand times thank you..............................................................................................................................................

    ReplyDelete
  9. int dia_add(int a[][])
    {
    int k=m=>n?n:m;
    for(i=0;i<k;i++)
    {
    sum=sum+a[i][i];
    }


    }

    ReplyDelete
  10. if the user wants to enter a number and replace the diagonal by this number what is the code

    ReplyDelete
  11. Thanks...
    your effort.. must be appreciated.

    ReplyDelete
  12. Thanks...
    Please provide me a program to print sum of the diagonal elements from both the sides.....

    ReplyDelete
  13. #include
    void main()
    {
    int a[10][10],i,j,k,m,n,sum,opp_sum;

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

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

    }
    else
    {
    printf("\nNot a square matrix");
    exit(0);
    }

    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
    {
    printf(" %d",a[i][j]);
    }
    printf("\n");
    }
    sum=0;
    opp_sum=0;
    for(i=0;i<m;++i)
    {
    sum=sum+a[i][i];
    opp_sum=opp_sum+a[i][m-i-1];
    }
    printf("\nThe Sum of the principle diagonal elements=%d\n",sum);
    printf("\nThe sum of the opposite diagonal elements=%d\n",opp_sum);
    }

    ReplyDelete
  14. thnx a lot :)

    ReplyDelete