Friday, August 20, 2010

TO FIND THE SUM OF DIAGONAL ELEMENTS OF THE SQUARE MATRIX

0 comments
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[3][3],i,j,s=0,r,c;
clrscr();
/*Enter how many rows and columns */
printf("Enter how many rows: ");
scanf("%d",&r);
printf("Enter how many columns: ");
scanf("%d",&c);
if(r==c)
{
/*enter the elements*/
printf("Enter the numbers: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
/*show the matrix elements*/
printf("The matrix is : ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%3d ",a[i][j]);
}
}
/*show the sum of  diagonal elements*/
printf(" Sum of  diagonal elements are: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i==j)
s=s+a[i][j];
}
}
{
printf(" %d",s);
}
}
getch(); 
}

Leave a Reply