Skip to content

Commit

Permalink
Create Find Sum of Diagonals elements in a Matrix.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
MonalikaKapoor authored May 26, 2021
1 parent 3a2c6c7 commit d7346c3
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Find Sum of Diagonals elements in a Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* C++ Program to Find Sum of Diagonals elements in a Matrix */

#include<iostream>

using namespace std;

int main()
{
int a[10][10],d1sum=0,d2sum=0,m,i,j;
cout<<"Enter size of matrix :: ";
cin>>m;
cout<<"\nEnter Elements to Matrix Below :: \n";

for(i=0;i<m;i++)
{
for(j=0;j<m;++j)
{
cout<<"\nEnter a["<<i<<"]["<<j<<"] Element :: ";
cin>>a[i][j];
}

}

cout<<"\nThe given matrix is :: \n\n";
for (i = 0; i < m; ++i)
{
for (j = 0; j < m; ++j)
{
cout<<"\t"<<a[i][j];
}
printf("\n\n");
}



for(i=0;i<m;++i)
for(j=0;j<m;++j)
{
if(i==j)
d1sum+=a[i][j];
if(i+j==(m-1))
d2sum+=a[i][j];
}

cout<<"\nSum of 1st diagonal is :: "<<d1sum;
cout<<"\n\nSum of 2nd diagonal is :: "<<d2sum;

return 0;
}

0 comments on commit d7346c3

Please sign in to comment.