Program to read 5*5 matrix and find sum of diagonal elements and to find sum of diagonal elements and all elements
/*Q.8.Program to read 5*5 matrix and find sum of diagonal elements and to find sum of diagonal elements and all elements
Programmed by: Jitendra Kr Yadav
Roll: 10116
Date: 2011/04/10*/
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5][5]={{1,2,3,4,5},{6,4,2,7,9},
{4,9,4,2,7},{6,4,3,8,9},{4,5,4,3,7}};
int s=0;
int i,j;
int sum = 0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
s= s+a[i][j];
printf(“%d\t”;a[i][j]);
if (i==j || i+j==4)
{
sum = sum + a[i][j];
}
}
printf(“\n”);
}
printf(“sum of diagonal elements %d\n”,sum);
printf (“sum of all elements %d\n”,s);
getch();
}
Result :-
sum of diagonal elements 44
sum of all elements 122