-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagonal_sum.cpp
51 lines (50 loc) · 955 Bytes
/
diagonal_sum.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
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
int main()
{
int m, n,lsum=0,rsum=0;
int arr[100][100];
cout << "Enter the rows : ";
cin >> m;
cout << "Enter the cloumns : ";
cin >> n;
for (int i = 0; i < m; i++)
{
int j;
for (j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
cout << "The Matrice is : " << endl;
for (int i = 0; i < m; i++)
{
int j;
for (j = 0; j < n; j++)
{
cout<<arr[i][j]<<" ";
}
cout << endl;
}
cout << endl;
if (m == n)
{
for (int i = 0; i < m; i++)
{
int j;
for (j = 0; j < n; j++)
{
if (i == j)
lsum += arr[i][j];
if (i + j == n - 1)
rsum += arr[i][j];
}
}
cout << "Left Diagonal sum : " << lsum << endl;
cout << "Right Diagonal sum : " << rsum << endl;
cout << "Total sum of Diagonals : " << lsum + rsum << endl;
}
else
cout << "Not a Square Matrice so sum of diagonals not possible" << endl;
return 0;
}