find sum of diagonal element of a matrix in c

Solutions on MaxInterview for find sum of diagonal element of a matrix in c by the best coders in the world

showing results for - "find sum of diagonal element of a matrix in c"
Valentina
12 Aug 2016
1#include <stdio.h>
2void main ()
3{
4    int i,j,matrix[10][10],m,n,sum=0;
5
6    printf("Enter number of rows of matrix  : ");
7    scanf("%d", &m);
8    printf("Enter number of columns of matrix  : ");
9    scanf("%d", &n);
10
11    printf("\n");
12
13    if(m==n)
14    {
15        for (i = 0; i < m; i++)
16        {
17            for (j = 0; j < n; j++)
18            {
19                printf("Enter element of matrix [%d][%d]: ", i, j);
20                scanf("%d", &matrix[i][j]);
21            }
22        }
23
24        printf("\n");
25
26        for(i=0;i<m;i++)
27        {
28            for(j=0;j<n;j++)
29            {
30                if(i==j)
31                {
32                    sum=sum+matrix[i][j];
33                }
34            }
35        }
36
37        printf("The sum of diagonal elements of the matrix = %d\n",sum);
38    }
39
40    else
41        printf("The matrix does not have diagonal elements");
42}
43
44
45
Laney
23 May 2019
1#include<stdio.h>
2 
3void main()
4{
5    int mat[12][12];
6    int i,j,row,col,sum=0;
7    printf("Enter the number of rows and columns for 1st matrix\n");
8    scanf("%d%d",&row,&col);
9    printf("Enter the elements of the matrix\n");
10    for(i=0;i<row;i++)
11    {
12        for(j=0;j<col;j++)
13        {
14            scanf("%d",&mat[i][j]);
15        }
16    }
17 
18    printf("The matrix\n");
19    for(i=0;i<row;i++)
20    {
21        for(j=0;j<col;j++)
22        {
23            printf("%d\t",mat[i][j]);
24        }
25        printf("\n");
26    }
27    //To add diagonal elements
28    for(i=0;i<row;i++)
29    {
30        for(j=0;j<col;j++)
31        {
32            if(i==j)
33            {
34                sum=sum+mat[i][j];
35            }
36        }
37    }
38 
39    printf("The sum of diagonal elements of a square matrix = %d\n",sum);
40}
41
queries leading to this page
sum of two diagonal elements of a matrix in csum of upper diagonal matrix in cprint diagonal elements of matrix in cc program to display diagonal elements of a matrixwap in c to find the sum of diagonal of a matrix using function sum of diagonal matrix carray diagonal elements sum in csum of diagonal matrix in ccalculate the sum from the diagonal in the matrix c 2b 2bdifference of diagonal elements of a matrix in cc program sum of diagonal elementswrite a program to find sum of diagonal elements of matrix in chow to add other diagonal elements of matrix in csum of each diagonal in csum of diagonal elements of a matrix using pointers in csum of main diagonal of a matrix in cc program for addition of diagonal of matrixprint all those elements of a matrix are not diagonal elements in chow to add diagonal elements of matrix in csum of diagonal elements of an array in csum of right diagonal matrix in csum of diagonal elements of a matrix in cproduct of diagonal elements of matrix in cwrite a programme to display element of left diagonal and right diagonal matrix sum display in chow to find sum of secondary diagonal elements in chow to set diagonal entries of a matrix equal to something in csum of second diagonal elements of a matrix in csum of secondary diagonal elements of a matrix in cmatrix diagonal sum in cprogram to print diagonal elements of a matrix in csum of principal diagonal elements in csum of diagonal elements of a matrix using do while loop chow to get diagonal elements of a matrix in cfind sum of diagonal element of a matrix in csum of opposite diagonal elements of a matrix in cto print diagonal elements of the matrix in cdiagonal addition of matrix in caddition of major diagonal elements c programfind the sum of diagonal elements of a matrix in cmartrix diagonal sum in cc program to print sum of diagonal elements in a matrixsum of left diagonal matrix in csum of diagonal in cfind diagonals of matrix in csum of both diagonal elements of a matrix in cfind sum of diagonal element of a matrix in c