-
Notifications
You must be signed in to change notification settings - Fork 130
/
Sum_Diagnols.c
40 lines (34 loc) · 970 Bytes
/
Sum_Diagnols.c
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
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
int main(){
int row, sum = 0;
printf("Input the rows and columns of a matrix: ");
scanf("%d", &row);
int **array = malloc(row * sizeof(int));
for (int i = 0; i < row; ++i){
array[i] = (int *)malloc(row * sizeof(int));
}
printf("\nInput the elements of the matrix:");
for (int i = 0; i < row; ++i){
for (int j = 0; j < row; ++j){
printf("\nelement - [%d] [%d]: ", i, j);
scanf("%d", &array[i][j]);
}
}
printf("\nThe matrix is:\n");
for (int i = 0; i < row; ++i){
for (int j = 0; j < row; ++j){
printf("%d\t", array[i][j]);
}
printf("\n");
}
for (int i = 0; i < row; ++i){
for (int j = 0; j < row; ++j){
if (i == j){
sum = sum + array[i][j];
}
}
}
printf("Addition of the diagnol elements is: %d\n", sum);
free(array);
}