C Program For SUM OF DIAGONAL ELEMENTS IN MATRIX
In this program, we have a square matrix mat of maximum size 10 x 10. The variable
size holds the value of size of matrix and the variable sum contains the sum of diagonal
elements of the matrix mat. Now, first we ask the user to enter the size of the matrix
and store the inputted value in the variable size. Next using a nested for loop we enter
the values in the matrix mat.
CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,a[10][10],z=0;
clrscr();
printf("Enter the r and c");
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(i==j)
{
z=z+a[i][j];
}
}
}
printf("sum of digonal elements=%d",z);
getch();
}

0 comments:
Post a Comment