-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ39.c
53 lines (45 loc) · 1.09 KB
/
Q39.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
41
42
43
44
45
46
47
48
49
50
51
52
53
//C program to find sum of main diagonal elements of a matrix
//C program to find sum of main diagonal elements of a matrix
#include<stdio.h>
int main()
{
int i,j,m,n,sum=0;
//Number of rows and columns should be same
printf("Enter the rows :");
scanf("%d",&m);
printf("Enter the columns :");
scanf("%d",&n);
int a[m][n];
if(m==n)
{
printf("Enter the elemnts in array :");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nCreated array :\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j || j+i==m-1)
{
sum+=a[i][j];
}
}
}
printf("sum of diagnal array : %d",sum);
}
return 0;
}