C program to find sum of diagonal elements of a matrices
Answers
Answer:
#include<stdio.h>
void main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
//To add diagonal elements
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\n",sum);
}
Explanation:
enter the numbers of rows and columns
2
2
enter the elements of matrix
4
5
7
8
the matrix
4 5
7 8
the sum of diagonal elements of a square matrix =4
the sum of diagonal elements of a square matrix
=12