-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumOfDiagonal.cpp
44 lines (37 loc) · 922 Bytes
/
sumOfDiagonal.cpp
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
#include <iostream>
using namespace std;
int main()
{
int arr[3][3] = {{12, 4, 5}, {32, 4, 21}, {5, 57, 44}};
int row = 3;
int col = 3;
int total = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
// for Diagonal elements
// for (int i = 0; i < row; i++)
// {
// for (int j = 0; j < col; j++)
// {
// if (i == j)
// total = total + arr[i][j];
// }
// }
// cout << "the total is for all diagonal elements: " << total << endl;
// for Reverse Diagonal Elements
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (i + j==2)
total = total + arr[i][j];
}
}
cout << "the total is for all diagonal elements: " << total << endl;
}