Monday, 16 March 2015

To find sum of all the elements of the given matrix Using C

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

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

RUN 1 :
~~~~~~~
Enter the order of the matrix : 3 3
Enter the elements of the matrix :
1 2 3
4 5 6
7 8 9
The sum of the elements in the matrix is : 45

No comments:

Post a Comment