Monday 16 March 2015

Find sum of diagonal elements of the given matrix Using C

# include <stdio.h>
# include <conio.h>

void main()
{
int mat[10][10] ;
int i, j, size, sum = 0 ;
clrscr() ;
printf("Enter size of the square matrix : ") ;
scanf("%d", &size) ;
printf("\nEnter the elements of the matrix : \n\n") ;
for(i = 0 ; i < size ; i++)
for(j = 0 ; j < size ; j++)
scanf("%d", &mat[i][j]) ;
for(i = 0 ; i < size ; i++)
sum = sum + mat[i][i] ;
printf("\nThe sum of diagonal elements in the matrix is : %d",
sum) ;
getch() ;
}

RUN 1 :
~~~~~~~
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The sum of diagonal elements in the matrix is : 15

No comments:

Post a Comment