Skip to content

Commit d7346c3

Browse files
Create Find Sum of Diagonals elements in a Matrix.cpp
1 parent 3a2c6c7 commit d7346c3

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* C++ Program to Find Sum of Diagonals elements in a Matrix */
2+
3+
#include<iostream>
4+
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int a[10][10],d1sum=0,d2sum=0,m,i,j;
10+
cout<<"Enter size of matrix :: ";
11+
cin>>m;
12+
cout<<"\nEnter Elements to Matrix Below :: \n";
13+
14+
for(i=0;i<m;i++)
15+
{
16+
for(j=0;j<m;++j)
17+
{
18+
cout<<"\nEnter a["<<i<<"]["<<j<<"] Element :: ";
19+
cin>>a[i][j];
20+
}
21+
22+
}
23+
24+
cout<<"\nThe given matrix is :: \n\n";
25+
for (i = 0; i < m; ++i)
26+
{
27+
for (j = 0; j < m; ++j)
28+
{
29+
cout<<"\t"<<a[i][j];
30+
}
31+
printf("\n\n");
32+
}
33+
34+
35+
36+
for(i=0;i<m;++i)
37+
for(j=0;j<m;++j)
38+
{
39+
if(i==j)
40+
d1sum+=a[i][j];
41+
if(i+j==(m-1))
42+
d2sum+=a[i][j];
43+
}
44+
45+
cout<<"\nSum of 1st diagonal is :: "<<d1sum;
46+
cout<<"\n\nSum of 2nd diagonal is :: "<<d2sum;
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)