Contact Learn C
Copy Program Share Program

Program 135:To find Sum of all Diagonals in a matrix

Program 135:
Method I
#include<stdio.h>
main()
{ 
 int i,j,rows,col,diagonal_1=0,diagonal_2=0,m,n;
 printf("Enter number of rows\n");
 scanf("%d %d",&rows,&col);

 
 int a[rows][col];
 if(col==rows)
 {
  m=0;
  n=rows-1;
   //Taking input for matrix
 printf("Enter Matrix 1\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   scanf("%d",&a[i][j]);
  }
  }
  printf("The given Matrix is\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
  }
  
  for(i=0;i<rows;i++)
  {
   diagonal_1+=a[i][i];
   diagonal_2+=a[m][n];
   m++;
   n--;
  }
  printf("Sum of diagonal is %d\n",diagonal_1);
  printf("Sum of another diagonal is %d\n",diagonal_2);
}
 else
 {
  printf("Not Possible\n");
 }
 
  
}

Method II 
 
#include<stdio.h>
 main()
{ 
 int i,j,rows,col,diagonal_1=0,diagonal_2=0;
 printf("Enter number of rows\n");
 scanf("%d %d",&rows,&col);

 
 int a[rows][col];
 if(col==rows)
 {
   //Taking input for matrix
 printf("Enter Matrix 1\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   scanf("%d",&a[i][j]);
  }
  }
  printf("The given Matrix is\n");
  for(i=0;i<rows;i++)
  {
  for(j=0;j<col;j++)
  {
   printf("%d\t",a[i][j]);
  }
  printf("\n");
  }
  
  for(i=0;i<rows;i++)
  {
   diagonal_1+=a[i][i];
   diagonal_2+=a[i][rows-i-1];
   
  }
  printf("Sum of diagonal is %d\n",diagonal_1);
  printf("Sum of another diagonal is %d\n",diagonal_2);
}
 else
 {
  printf("Not Possible\n");
 }
 
  
}

Explanation:

//Coming Soon...

Output:

To find Sum of all Diagonals in a matrix


















 

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide