EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

Write a program to find the sum of the diagonals of the given matrix

Source Code

#include<stdio.h>
#include<conio.h>
int main (int argc, char *argv[])
{
  clrscr();
  int m[10][10];
  int i,j,n,sld=0,srd=0,sd=0,t=0;
  for(i=0;i<10;i++)
  {
10        for(j=0;j<10;j++)
11        {
12        m[i][j]=0;
13        }
14    }
15    printf("\n Enter size of a square matrix:");
16    scanf("%d",&n);
17    printf("\n Enter the two matrices: \n");
18    for(i=0;i<n;i++)
19    {
20        for(j=0;j<n;j++)
21        {
22      printf("Enter element %d %d of m1: ",(i+1),(j+1));
23      scanf("%d",&m[i][j]);
24        }
25    }
26    printf("\n The matrix is: \n");
27    for(i=0;i<n;i++)
28    {
29        for(j=0;j<n;j++)
30        {
31       printf("%d ",m[i][j]);
32        }
33        printf("\n");
34    }
35    for(i=0;i<n;i++)
36    {
37        sld+=m[i][i];
38        srd+=m[i][n-i-1];
39    }
40    if(n%2!=0)
41    {
42      t=n/2;
43      sd=sld+srd-m[t][t];
44    }
45    else
46      sd=sld+srd;
47    printf("\n The sum of diagonals of matrix are:\n %d : left diagonal\n %d : right diagonal\n %d : sum of the two diagonals",sld,srd,sd);
48    getch();
49  }
50 

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code