-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Find Sum of Diagonals elements in a Matrix.cpp
- Loading branch information
1 parent
3a2c6c7
commit d7346c3
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |