Write a program to find out sum of diagonal element of a matrix


Default
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Write a program to find out sum of diagonal element of a matrix
// Author: AimForJob.com
#include
int main(){
     int row_size,col_size,i=0,j=0;
     printf("Enter Matrix row size:");
     scanf("%d",&row_size);                 // matrix row size input from user
     printf("Enter Matrix column size:");  
     scanf("%d",&col_size);                 // matrix column size input from user
     int matrix[row_size][col_size];
    
     // user input for matrix elements
     printf("Enter Matrix Elements");
     for(i=0;i<row_size;i++){
             for(j=0;j<col_size;j++){
                     scanf("%d",&matrix[i][j]);
             }
     }
    
     // sum of diagonal elements
     int sum=0;
     int output[row_size][col_size];
     for(i=0;i<row_size;i++){
             sum = sum+matrix[i][i];
     }
    
     // printing sum of diagonal elements
printf("Sum of diagonal elements: %d",sum);
}

No comments:

Post a Comment