C program to find accept a matrix of order M x N and find the sum of the main diagonal and off diagonal elements

by Nideesh C on April 13, 2011 · 0 comments

in C Programming




/* C program to find accept a matrix of order M x N and find the sum of the  main diagonal and off diagonal elements  */

#include <stdio.h>

void main ()
{
static int ma[10][10];
int i,j,m,n,a=0,sum=0;

printf (“Enter the order of the matrix \n”);
scanf (“%d %d”,&m,&n);

if ( m == n )
{
printf (“Enter the co-efficient s of the matrix\n”);

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf (“%d”,&ma[i][j]);
}
}

printf (“The given matrix is \n”);
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (” %d”,ma[i][j]);
}
printf (“\n”);
}

for (i=0;i<m;++i)
{
sum = sum + ma[i][i];
a = a + ma[i][m-i-1];
}

printf (“\nThe sum of the main diagonal elements is = %d\n”,sum);
printf (“The sum of the off diagonal elements is   = %d\n”,a);
}
else
printf (“The given order is not square matrix\n”);
}                 /* End of main() */

/*——————————————————–
Output
Enter the order of the matrix
3 3
Enter the co-efficient s of the matrix
1 2 3
4 5 6
7 8 9
The given matrix is
1 2 3
4 5 6
7 8 9

The sum of the main diagonal elements is = 15
The sum of the off diagonal elements is   = 15

Run 2
Enter the order of the matrix
2 3
The given order is not square matrix

——————————————————–*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept a matrix of order M x N and store its elements and interchange the main diagonal elements of the matrix with that of the secondary diagonal elements
  2. C program to read a matrix A (MxN) and to find the following using functions a) Sum of the elements of each row b) Sum of the elements of each column c) Find the sum of all the elements of the matrix Output the computed results with suitable headings
  3. C program to accept a matrix of order M x N and find the sum of each row and each column of a matrix
  4. Sample – Program to find the sum of each row & column of a matrix of size n x m and if matrix is square, find the sum of the diagonals also..
  5. Sample – Program to find the sum of either of the diagonals of a 4 x 4 matrix.

Leave a Comment

Previous post:

Next post: