-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2D_array.cpp
More file actions
21 lines (20 loc) · 732 Bytes
/
2D_array.cpp
File metadata and controls
21 lines (20 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Calculate the sum of elements in the main diagonal of a 2D array.
#include <iostream>
using namespace std;
int main() {
// Define a 2D array
int arr[3][3] ={{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int sum = 0;
int n;
cout << "Enter a value of n : ";
cin >> n;
// Calculate the sum of elements in the main diagonal
for (int i = 0; i < n; i++) {
sum += arr[i][i];
}
// Output the sum
cout << "Sum of elements in the main diagonal: " << sum << endl;
return 0;
}