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++)
  {
      for(j=0;j<10;j++)
      {
      m[i][j]=0;
      }
  }
  printf("\n Enter size of a square matrix:");
  scanf("%d",&n);
  printf("\n Enter the two matrices: \n");
  for(i=0;i<n;i++)
  {
      for(j=0;j<n;j++)
      {
    printf("Enter element %d %d of m1: ",(i+1),(j+1));
    scanf("%d",&m[i][j]);
      }
  }
  printf("\n The matrix is: \n");
  for(i=0;i<n;i++)
  {
      for(j=0;j<n;j++)
      {
     printf("%d ",m[i][j]);
      }
      printf("\n");
  }
  for(i=0;i<n;i++)
  {
      sld+=m[i][i];
      srd+=m[i][n-i-1];
  }
  if(n%2!=0)
  {
    t=n/2;
    sd=sld+srd-m[t][t];
  }
  else
    sd=sld+srd;
  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);
  getch();
}

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