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









18 comments:

MyClipta said...

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

Anonymous said...

thank u................

esha said...

can u give d code using pointers???

sujeet said...

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

Anonymous said...

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.

Neeraja Kadiri said...

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

satish kumar sms collage varanasi said...

#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();
}

Ashish rathor said...

nice....

Anonymous said...

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

Anonymous said...

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


}

Anonymous said...

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

Anonymous said...

Thanks...
your effort.. must be appreciated.

Katty9930 said...

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

Dhanya said...

#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);
}

Anonymous said...

thnx a lot :)

Haridas Roy said...

lot of thanxxxx,,,,
try the program run

Unknown said...

Create a 12 x 12 matrix for representing a multiplication table, where the i, jth element of the matrix stores the value i * j.

Zobayer said...

Thanks