DIAGONAL ELEMENTS

Consider a matrix and print principal diagonal and opposite diagonal from the given matrix.
Principal Diagonal:
Condition is if row number is equal to column number ,then it is principal diagonal.


1
2
3
4
5
1
10
20
30
40
50
2
11
12
13
14
15
3
21
22
23
24
25
4
31
32
33
34
35
5
41
42
43
44
45
Principal diagonal is represented in colors and by the pattern we can observe that "i" is equal is "j"

i
j
1
1
2
2
3
3
4
4
5
5
int a[n][n];
int i,j;
for(i=n-1;i>=0;i--)
for(j=n-1;j>=0;j--)
if(i = = j)
printf("%d",a[i][j]);
The explanation is if i value is equal to j then print value of matrix
Opposite side of diagonal is

1
2
3
4
5
1
10
20
30
40
50
2
11
12
13
14
15
3
21
22
23
24
25
4
31
32
33
34
35
5
41
42
43
44
45
The pattern is

i
j
1
5
2
4
3
3
4
2
5
1
The condition here is i+j-1 is equal to range that is 5.
int a[n][n];
int i,j;
for(i=n-1;i>=0;i--)
for(j=n-1;j>=0;j--)
if(i+j-1 = = n)
printf("%d",a[i][j]);