Answers for "find sum of diagonal element of a matrix in c"

C
0

find sum of diagonal element of a matrix in c

#include <stdio.h>
void main ()
{
    int i,j,matrix[10][10],m,n,sum=0;

    printf("Enter number of rows of matrix  : ");
    scanf("%d", &m);
    printf("Enter number of columns of matrix  : ");
    scanf("%d", &n);

    printf("\n");

    if(m==n)
    {
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("Enter element of matrix [%d][%d]: ", i, j);
                scanf("%d", &matrix[i][j]);
            }
        }

        printf("\n");

        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                {
                    sum=sum+matrix[i][j];
                }
            }
        }

        printf("The sum of diagonal elements of the matrix = %d\n",sum);
    }

    else
        printf("The matrix does not have diagonal elements");
}
Posted by: Guest on June-25-2021
0

sum of diagonal elements of a matrix in c

#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);
}
Posted by: Guest on April-01-2021

Code answers related to "find sum of diagonal element of a matrix in c"

Code answers related to "C"

Browse Popular Code Answers by Language